Displaying MySQL Records Using PHP - Page 2
         by njs12345

Source Files
First you should download the source files, which you can find here.
By the end of this tutorial, you should have created something like this:


Making it Work
Open up PHPEdit and open the document mysql.php. The code you will need to write or copy/paste is in the text box below. Paste it in between the first <tr> and the ending </table>.


Code Explanation:

$connection = mysql_connect("localhost", "e_novative", "e_novative") or die("Error connecting to database");

This code connects to the database located on localhost (i.e this computer) using the username e_novative, and the password e_novative. This is the default username/password combination that comes with WAMP. Note that this must be put in a variable, in this case, $connection. Many functions require a variable like this. The or die(); part just means "If there is an error, exit the script providing this error message:".

mysql_select_db("members", $connection) or die("Error selecting database");

This sets the database that the query will be used on as members, the database we created earlier. It also uses the $connection variable that holds the connection to the MySQL database.

$result = mysql_query("SELECT * FROM members ORDER BY id", $connection) or die("Error querying database");

This line also uses the $connection variable. This time it is querying the database using a language called SQL. That SQL statement simply means "Select everything from the table members, ordering the records by the field id". The result of this query is stored in the imaginatively named variable $result.

$i = 0;
while($result_ar = mysql_fetch_assoc($result)){

In the variable result, there is a pointer which stores the current record being processed. The mysql_fetch_assoc function returns false if the pointer is at the end of the recordset, or true if the pointer is on a usable record. It also creates an associative array with the names of the fields being the keys, and the values of the array being the values, which in this case is stored in $result_ar. The variable $i is simply a counter variable.

< tr <?php if($i%2 == 1){
echo "class='body2'";
echo "class='body1'";}?>>

We then exit out of PHP. But wait! We didn't finish off the while loop, which means that PHP echoes out what comes after once for every iteration of the while loop. The next part simply echoes the start of a table row, then chooses one CSS class depending on whether $i (our counter variable) is even or odd.


<?php echo $result_ar['name']; ?></td>
<?php echo $result_ar['email']; ?>
<?php echo $result_ar['website']; ?>

This part makes up the rows of our data grid, by echoing three fields, name, email and website, from the associative array we defined earlier in the test for our while loop.


Finally, we finish the while loop, incrementing the counter variable by 1.

If you need help, feel free to post in the Server-Side/Scripting forum!

Previous Page  



kirupa.com's fast and reliable hosting provided by Media Temple.