PHP | MySQL Select Query Last Updated : 26 Jul, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report The SQL SELECT statement is used to select the records from database tables. Syntax : The basic syntax of the select clause is - To select all columns from the table, the * character is used. Implementation of the Select Query : Let us consider the following table ' Data ' with three columns ' FirstName ', ' LastName ' and ' Age '. To select all the data stored in the ' Data ' table, we will use the code mentioned below. SELECT Query using Procedural Method : CPP <?php $link = mysqli_connect("localhost", "root", "", "Mydb"); if ($link === false) { die("ERROR: Could not connect. " .mysqli_connect_error()); } $sql = "SELECT * FROM Data"; if ($res = mysqli_query($link, $sql)) { if (mysqli_num_rows($res) > 0) { echo "<table>"; echo "<tr>"; echo "<th>Firstname</th>"; echo "<th>Lastname</th>"; echo "<th>age</th>"; echo "</tr>"; while ($row = mysqli_fetch_array($res)) { echo "<tr>"; echo "<td>".$row['Firstname']."</td>"; echo "<td>".$row['Lastname']."</td>"; echo "<td>".$row['Age']."</td>"; echo "</tr>"; } echo "</table>"; mysqli_free_result($res); } else { echo "No matching records are found."; } } else { echo "ERROR: Could not able to execute $sql. " .mysqli_error($link); } mysqli_close($link); ?> Output : Code Explanation: The "res" variable stores the data that is returned by the function mysql_query(). Everytime mysqli_fetch_array() is invoked, it returns the next row from the res() set. The while loop is used to loop through all the rows of the table "data". SELECT Query using Object Oriented Method : html <?php $mysqli = new mysqli("localhost", "root", "", "Mydb"); if ($mysqli === false) { die("ERROR: Could not connect. " .$mysqli->connect_error); } $sql = "SELECT * FROM Data"; if ($res = $mysqli->query($sql)) { if ($res->num_rows > 0) { echo "<table>"; echo "<tr>"; echo "<th>Firstname</th>"; echo "<th>Lastname</th>"; echo "<th>Age</th>"; echo "</tr>"; while ($row = $res->fetch_array()) { echo "<tr>"; echo "<td>".$row['Firstname']."</td>"; echo "<td>".$row['Lastname']."</td>"; echo "<td>".$row['Age']."</td>"; echo "</tr>"; } echo "</table>"; $res->free(); } else { echo "No matching records are found."; } } else { echo "ERROR: Could not able to execute $sql. " .$mysqli->error; } $mysqli->close(); ?> Output : SELECT Query using PDO Method : html <?php try { $pdo = new PDO("mysql:host = localhost; dbname=mydb", "root", ""); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } catch (PDOException $e) { die("ERROR: Could not connect. ".$e->getMessage()); } try { $sql = "SELECT * FROM Data"; $res = $pdo->query($sql); if ($res->rowCount() > 0) { echo "<table>"; echo "<tr>"; echo "<th>Firstname</th>"; echo "<th>Lastname</th>"; echo "<th>Age</th>"; echo "</tr>"; while ($row = $res->fetch()) { echo "<tr>"; echo "<td>".$row['Firstname']."</td>"; echo "<td>".$row['Lastname']."</td>"; echo "<td>".$row['Age']."</td>"; echo "</tr>"; } echo "</table>"; unset($res); } else { echo "No matching records are found."; } } catch (PDOException $e) { die("ERROR: Could not able to execute $sql. " .$e->getMessage()); } unset($pdo); ?> Output : Comment More infoAdvertise with us Next Article PHP | MySQL Select Query S Shubrodeep Banerjee Follow Improve Article Tags : PHP mysql Similar Reads PHP Tutorial PHP is a widely used, open-source server-side scripting language primarily designed for web development. It is embedded directly into HTML and generates dynamic content on web pages. It allows developers to handle database interactions, session management, and form handling tasks.PHP code is execute 9 min read MySQL | Regular Expressions (Regexp) In MySQL, regular expressions (REGEX) offer powerful functionality for flexible pattern matching within string data. By using the REGEXP and RLIKE operators, developers can efficiently search, validate, and manipulate string data in more dynamic ways than simple LIKE queries. In this article, we wil 6 min read SQL HAVING Clause with Examples The HAVING clause in SQL is used to filter query results based on aggregate functions. Unlike the WHERE clause, which filters individual rows before grouping, the HAVING clause filters groups of data after aggregation. It is commonly used with functions like SUM(), AVG(), COUNT(), MAX(), and MIN(). 4 min read SQL Concepts and Queries In this article, we will discuss the overview of SQL and will mainly focus on Concepts and Queries and will understand each with the help of examples. Let's discuss it one by one.Overview :SQL is a computer language that is used for storing, manipulating, and retrieving data in a structured format. 5 min read Top 60+ PHP Interview Questions and Answers -2025 PHP is a popular server-side scripting language, widely known for its efficiency in web development and versatility across various platforms. PHP is extensively utilized by top companies such as Facebook, WordPress, Slack, Wikipedia, MailChimp, and many more due to its robust features and high perfo 15+ min read PHP Introduction PHP stands for Hypertext Preprocessor. It is an open-source, widely used language for web development. Developers can create dynamic and interactive websites by embedding PHP code into HTML. PHP can handle data processing, session management, form handling, and database integration. The latest versi 8 min read MySQL | LEAD() and LAG() Function The LEAD() and LAG() functions in MySQL are powerful window functions introduced in version 8.0. They allow users to access data from preceding or following rows without the need for self-joins or subqueries, making them invaluable for analyzing sequential or time-series data.In this article, we wil 3 min read Version Control Systems Version Control Systems (VCS) are essential tools used in software development and collaborative projects to track and manage changes to code, documents, and other files. Whether you're working alone or as part of a team, version control helps ensure that your work is safe, organized, and easy to co 7 min read MySQL | PARTITION BY Clause A PARTITION BY clause is used to partition rows of table into groups. It is useful when we have to perform a calculation on individual rows of a group using other rows of that group. It is always used inside OVER() clause. The partition formed by partition clause are also known as Window. This claus 2 min read PHP Arrays Arrays are one of the most important data structures in PHP. They allow you to store multiple values in a single variable. PHP arrays can hold values of different types, such as strings, numbers, or even other arrays. Understanding how to use arrays in PHP is important for working with data efficien 5 min read Like