How To Select Data from a MySQL Table in PHP

Introduction: This tutorial is on how to list data from a MySQL table. HTML: Before we begin, we are going to create some basic HTML code to correctly format our information we will be listing later. For this, we are going to first create the basic HTML layout...
  1. <head>
  2. </head>
  3. <body>
  4. </body>
  5. </html>
Next we are going to create a div tag to hold our information. This goes within our 'body' tags as it is part of the page design, and not performing code...
  1. <div class='information'>
  2. </div>
Within that div tag, we are going to put our listing PHP code. PHP: Before we write out our listing PHP code, we want to create the connection to our database. We put this within PHP tags at the top of our HTML/PHP document. We use the 'mysqli_connect' function to make the connection, takes four string parameters of; service server, username, password, database name. And we store this connection in to a new variable named 'con' ($con)...
  1. <?php
  2. $con = mysqli_connect('localhost', 'root', '', 'testDB');
  3. ?>
The above code creates a connection in our '$con' variable object, to the service server 'localhost', with a username of 'root', no password, and the database named is 'testDB'. I am running this tutorial off of my previous tutorial which was insertting data in to a MySQL database/table from a user's HTML form. That table held two basic text (VARCHAR) information; username, and email. You can find that tutorial here; http://www.sourcecodester.com/tutorials/php/7751/how-insert-data-mysqli-database-through-php.html. As you can see from the tutorial, we have a database (DB) named 'testDB', and a table within that named 'testTable' with columns of; 'id' 'username' 'email' Now we are ready for our listing PHP code. Listing PHP Code: As stated earlier, we are going to throw this code in to our HTML div tags because that is where we want our table information to display on our HTML page. So first we create the basic PHP code tags...
  1. <?php
  2.  
  3. ?>
Next we are going to run and store a query in a new variable/object named 'q' (standing for 'query'). The query function we will use is 'mysqli_query' which accepts two parameters; the database connection we created earlier and stored within our 'con' variable, and a query string. The query string we are going to use will simply select everything from our 'testTable' table. To do this, we use the following code...
  1. $q = mysqli_query($con, "SELECT * FROM `testTable`");
The * wildcard selects all the rows from the given table named (following the 'FROM' keyword). If we wanted to select only rows with the username of 'Yorkiebar', we would add a 'WHERE' clause to our query string to compare the username column value for each row to the string of 'Yorkiebar' we want...
  1. $q = mysqli_query($con, "SELECT * FROM `testTable` WHERE `username`='Yorkiebar'");
We could also order our results in a-z order like so...
  1. $q = mysqli_query($con, "SELECT * FROM `testTable` WHERE `username`='Yorkiebar' ORDER BY `username` DESC");
Once we have the information from our query, we want to ensure that the query ran correctly. If it did not run correctly, it will simply return an error/'false'. Because it returns 'false', we can check it directly by using an 'if' statement, like so...
  1. if ($q) {
  2.  
  3. }else
  4. echo 'Query failed.';
If the query did run successfully, we next want to ensure that at least one row was returned, if it wasn't, we output another error stating that no matching rows to thq given query were found...
  1. if (mysqli_num_rows($q) > 0) {
  2.  
  3. }else
  4. echo 'No matching rows to the given query were found.';
No rows would be returned if, for example, we used the above query searching for all rows where the username was 'Yorkiebar' but the only row within the table was with the username of 'Admin'. Finally, we want to iterate through each row of information returned (using a 'while' loop) and output each row of information to the HTML client-side of code using the PHP 'echo' statement...
  1. while ($row = mysqli_fetch_array($q)) {
  2. echo 'Username: '.$row["username"].', Email: '.$row["email"];
  3. }
We use indexing of the column name from the $row iterating variable within our while loop to grab the information within that field of the currently iterating row. Finished!

Add new comment