MySQL Select Statement using PHP with Source Code
What is MySQL Select Statement?
MySQL Select statement is used in Selecting one or more records in MySQL database, the SELECT statement is being used.
Examples of MySQL Select Statement using PHP in MySQLi OOP, MySQLi Procedural, and PDO
Syntax:
SELECT * from tableName;or
SELECT column1,column2 from tableName;The asterisks(*) symbol in our first example syntax represent all column in the database table.
In the second example, we specify the column name of a table.
Example using MySQLi (Object-Oriented):
<?php
$server = "localhost";
$username = "root";
$password = "";
$database = "mysqltuts";
// Create connection
$conn = mysqli_connect(server, username, password, database);
// Check connection
if (!conn) {
die("Connection failed: " . mysqli_connect_error());
}
";
?>Code explanation:
We just simply Select the Last name, First Name and Address of a person from our table, then we execute the query and store the results to the “$result” variable.
Next, using the num_rows function check if the result is greater than zero.
And if the result is greater than zero, the fetch_assoc() function will store the result into an associative array so that we will be able to loop through the records.
Get JOKEN VILLANUEVA’s stories in your inbox
Join Medium for free to get updates from this writer.
The While Loop Loops through the result set and display the result inside the table data because we use table to display the result.
Example using MySQLi(Procedural):
<?PHP
$server = "localhost";
$username = "root";
$password = "";
$database = "mysqltuts";
// Create connection
$conn = mysqli_connect(server, username, password, database);
// Check connection
if (conn) {
die("Connection failed: " . mysqli_connect_error());
}
// sql to Insert record table
$sql = "SELECT `LNAME`, `FNAME`, `ADDRESS` from `tblpeople`";
$results = mysqli_query(conn , sql);
echo "
";
?>
<table border='1'>
<tr>
<th>LAST NAME</th>
<th>FIRST NAME</th>
<th>ADDRESS</th>
</tr>
<tbody>
<?php
$row["ADDRESS"] . "</td>
}
} else {
echo "0 results";
}
?>
</tbody>
</table>
<?php
mysqli_close($conn);
?>Example using PDO:
$sql = "SELECT `LNAME`, `FNAME`, `ADDRESS` from `tblpeople`";
$query = conn->prepare( sql );
$query->execute();
$results = query->fetchAll( PDO::FETCH_ASSOC );
?>
foreach( results as $row ){
echo "";
}
conn = null;
echo "
<table border="1">
<tbody>
<tr>
<th>LAST NAME</th>
<th>FIRST NAME</th>
<th>ADDRESS</th>
</tr>
<tr>
<td>" $row["LNAME"]. "</td>
<td>" $row["FNAME"]. "</td>
<td>" $row["ADDRESS"]. "</td>
</tr>
</tbody>
</table>
";
?>The Output of the Code above looks as shown below.
📌Here’s the complete source code:
Conclusion
If you find this article valuable, please consider leaving a comment below and share your thoughts.
Your feedback will not only helps us to improve our content but also benefits others in the community by providing diverse insights and experiences.
Itsourcecode.com🚀
Thank you for being a part of the Itsourcecode community!
Before you leave, please consider the following:
I would appreciate it if you could show your support by clapping 50 times and following the author.
Follow us on [Pinterest]
Follow us on [KO-FI]
Follow us on [Facebook]


![Top 50+ Final Year Projects & Ideas for Computer Science with Source Code[2026]](https://miro.medium.com/v2/resize:fit:1358/format:webp/1*0N5Ajiqsllf2zE9e-jtJPA.png)


![TOP 15 ASP. Net Projects and Ideas with Free Source Code [2026]](https://miro.medium.com/v2/resize:fit:1358/format:webp/1*GvHyTZs3eTAXHZEeuFw6Pw.png)



