Home arrow PHP arrow Page 5 - Mastering WHILE Loops for PHP and MySQL

Retrieving All Rows in a MySQL Table Using WHILE Loops - PHP

Do you want to learn how to handle PHP WHILE loops? WHILE loops are one of the most powerful features as well as the easiest loop available to any PHP/MySQL developer. They enable us to shorten repetitive tasks for a highly useful application. This tutorial gives examples of WHILE loops in PHP/MySQL that beginner and novice developers can use as a quick reference for building similar loops in their applications.

TABLE OF CONTENTS:
  1. Mastering WHILE Loops for PHP and MySQL
  2. Using WHILE Loops as Counters
  3. Generating Unique Random Numbers Using WHILE Loops
  4. Constructing Trigonometric Tables Using WHILE Loops
  5. Retrieving All Rows in a MySQL Table Using WHILE Loops
By: Codex-M
Rating: starstarstarstarstar / 9
October 27, 2009

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

One of the classic PHP-MySQL applications is retrieving all rows in the MySQL table and outputting them as an HTML table. Consider the MySQL table “data” screen shot from phpMyadmin:

You like to display this entire MySQL table as an HTML table in the browser. The first thing you need to do is connect to the MySQL database.

$username = "yourmysqlusername";

$password = "yourmysqlpassword";

$hostname = "yourmysqlhostname";

$database = "yourmysqldatabase";

$dbhandle = mysql_connect($hostname, $username, $password)

or die("Unable to connect to MySQL");

$selected = mysql_select_db($database,$dbhandle)

or die("Could not select $database");

Next, we query MySQL to retrieve all entries:

$query= ‘SELECT * FROM data’;

$result = mysql_query($query)

or die (‘Error in query’);

Okay, now we need to display the HTML table:

echo ‘<table width=100% border=1>’;

echo ‘<tr><td><b>Student Name</b></td><td><b>Math Score</b></td><td><b>Arts Score</b></td><td><b>Philosophy Score</b></td></tr>’;

Finally, the WHILE loop can be used to retrieve the rows from $result, which is the result of the MySQL query:

While ($row=mysql_fetch_row($result))

{

echo ‘<tr>’;

echo ‘<td>’.$row[0].’</td>’;

echo ‘<td>’.$row[1].’</td>’;

echo ‘<td>’.$row[2].’</td>’;

echo ‘<td>’.$row[3].’</td>’;

}

echo ‘</table>’;

Free result set:

mysql_free_result($result);

Close database connection

mysql_close($dbhandle)

Combining all the scripts for a complete script:

<?php

$username = "xxx";

$password = "xxx";

$hostname = "xxx";

$database = "xxx";

$dbhandle = mysql_connect($hostname, $username, $password)

or die("Unable to connect to MySQL");

$selected = mysql_select_db($database,$dbhandle)

or die("Could not select $database");

$query= 'SELECT * FROM data';

$result = mysql_query($query)

or die ('Error in query');

echo '<table width=100% border=1>';

echo '<tr><td><b>Student Name</b></td><td><b>Math Score</b></td><td><b>Arts Score</b></td><td><b>Philosophy Score</b></td></tr>';

while ($row=mysql_fetch_row($result))

{

echo '<tr>';

echo '<td>'.$row[0].'</td>';

echo '<td>'.$row[1].'</td>';

echo '<td>'.$row[2].'</td>';

echo '<td>'.$row[3].'</td>';

echo '</tr>';

}

echo '</table>';

mysql_free_result($result);

mysql_close($dbhandle);

?>

This is how it looks in the browser.



 
 
>>> More PHP Articles          >>> More By Codex-M
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: