Retrieving multiple rows for data from a table
|
In the earlier tutorial we have retrieved a single row from a table, In this section we will learn how to retrieve multiple rows of data from a table.
As you have seen earlier we have used the function mysql_fetch_row to retrieve a single rows we use the mysql_fetch_array function to retrieve multiple rows from a table.
Here is an example where we retrieve the entire contents from our jokes table using mysql_fetch_array
<?php
$db = mysql_connect("localhost","root","pass");
mysql_select_db("mybuddy",$db);
//replace the above values with your actual database values
$sql_result = mysql_query("SELECT joke_no, joke FROM jokes", $db);
while($rs = mysql_fetch_array($sql_result))
{
echo "Joke No: $rs[0]<BR>";
echo str_replace("\n","<BR>",$rs[1]);
echo "<P>";
}
?>
There is nothing special here in the above example, except that mysql_fetch_row is called in a while loop so that it will continue the loop until all the records are displays (SQL Query is completed)
The mysql_fetch_array function accepts a result set as a parameter (stored in the $sql_result variable in this case), and returns the next row in the result set as an array this continues until there are no more rows in the result set when there are no more rows left mysql_fetch_array returns false which exits the loop.
Home | Privacy Policy | Contact Us | Terms of Service
(c) 2002 - 2019 www.PHPbuddy.com Unauthorized reproduction/replication of any part of this site is prohibited.
|