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 UPDATE Query S Shubrodeep Banerjee Follow Improve Article Tags : PHP mysql Similar Reads PHP | MySQL UPDATE Query The MySQL UPDATE query is used to update existing records in a table in a MySQL database. It can be used to update one or more field at the same time. It can be used to specify any condition using the WHERE clause. Syntax : The basic syntax of the Update Query is - Implementation of Where Update Que 2 min read PHP - MySQL : Nested Query In this article, we are going to perform nested query operations on the database in the MySQL server using the Xampp server. Introduction : PHP stands for hypertext preprocessor, which is a server-side scripting language and also used to handle database operations. We are a PHP xampp server to commu 5 min read PHP | MySQL ( Creating Table ) What is a table? In relational databases, and flat file databases, a table is a set of data elements using a model of vertical columns and horizontal rows, the cell being the unit where a row and column intersect. A table has a specified number of columns, but can have any number of rows. Creating a 3 min read MySQL | Common MySQL Queries MySQL server is a open-source relational database management system which is a major support for web based applications. Databases and related tables are the main component of many websites and applications as the data is stored and exchanged over the web. Even all social networking websites mainly 9 min read How to count rows in MySQL table in PHP ? PHP stands for hypertext preprocessor. MySQL is a database query language used to manage databases. In this article, we are going to discuss how to get the count of rows in a particular table present in the database using PHP and MySQL. Requirements: XAMPP Approach: By using PHP and MySQL, one can p 3 min read PHP - Mysql Joins In this article, we are going to join two tables using PHP and display them on the web page. Introduction : PHP is a server-side scripting language, which is used to connect with databases. Using this, we can get data from the database using PHP scripts. The database language that can be used to com 9 min read Like