PHP Tutorial - Displaying Query Results

OK, this tutorial assumes you have taken the mysql basic commands tutorial, or are familiar with connecting to and selecting a database, and running queries. Whilst there are a few ways to display the results of a query using PHP, I’m going to show you, i am going to show you (in my opinion) the easiest method - gathering the results of the query into an array.

Example Database

My database is called ‘ex’ and my table is called ‘userdb’ - and is set out as shown below:

id uname upass uemail
1 Huscy password davidhuscroft@hotmail.com
2 s73ad password another_user@msn.com

The Code So Far

I have slightly altered the code from the previous tutorial, by putting some values into variables, as seen below:

$host = ‘localhost‘ ;
$user = ‘huscy‘ ;
$pass = ‘password‘ ;
$db = ‘ex‘ ;
$table = ‘userdb‘ ;
$show_all = ‘SELECT * FROM tablename‘ ;

mysql_connect ($host,$user,$pass) or die ( mysql_error ());
mysql_select_db ($db)or die ( mysql_error ());
mysql_query ($show_all) or die ( mysql_error ());

Getting the result into an array

Now then, the next step is to change the last line to put the results of the query into a variable, as shown below:
$result = ‘ mysql_query ($show_all) or die(mysql_error()) ‘;
Now the results of the query are in a variable called $result, we can now use the mysql_fetch_array() function.

The syntax for mysql_fetch_array is `mysql_fetch_array ( resource result [, int result_type])` - so basically you put $result into the function, but we are going to go one step further, and use a while loop to print out everything in the database… here we go:

while ($row = mysql_fetch_array ($result))
{
$view_db = ‘ID NUMBER: ‘.$row[ ‘id‘ ];
$view_db .= ‘USERNAME: ‘.$row[ ‘uname‘ ];
$view_db .= ‘PASSWORD: ‘.$row[ ‘upass‘ ];
$view_db .= ‘EMAIL: ‘.$row[ ‘uemail‘ ];
}
echo ( ‘$view_db’ );

An Explanation

The above code simply states that while a variable $row (which could be anything, i guess i was taught to use row and it stuck?) is equal to an array of the results of the query - so now you have an array named $row, with the same structure as your database - nice eh? The next bit of code, inside the curly brackets, makes a new variable, called $view_db - which when printed will print the contents of the database.

In case you are not familiar with it, the .= is used to add to a variable - and is very useful in this case, as you can simply add to the $view_db variable each time the function loops, then echo the variable once.

All Together Now - Here is our complete code, and what will be displayed
$host = ‘localhost’ ;
$user = ‘huscy’ ;
$pass = ‘password’ ;
$db = ‘ex’ ;
$table = ‘userdb’ ;
$show_all = ‘SELECT * FROM $table’ ;

mysql_connect ($host,$user,$pass) or die ( mysql_error ());
mysql_select_db ($db)or die ( mysql_error ());
$result = mysql_query ($show_all) or die ( mysql_error ());
while ($row = mysql_fetch_array ($result))
{
$view_db .= ‘ID NUMBER: ‘.$row[ ‘id’ ].’<br>’;
$view_db .= ‘USERNAME: ‘.$row[ ‘uname’ ].’<br>’;
$view_db .= ‘PASSWORD: ‘.$row[ ‘upass’ ].’<br>’;
$view_db .= ‘EMAIL: ‘.$row[ ‘uemail’ ].’<br><br>’;
}
echo ( ‘$view_db’ );

ID NUMBER: 1
USERNAME: Huscy
PASSWORD: password
EMAIL ADDRESS: davidhuscroft@hotmail.comID NUMBER: 2
USERNAME: s73ad
PASSWORD: password
EMAIL ADDRESS: another_user@msn.com
Rating: