O'Reilly logo

Managing & Using MySQL, 2nd Edition by Hugh E. Williams, Randy Yarger, George Reese, Tim King

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Handling Results

The second MySQL library function called in showgifts( ) returns the number of rows that have been output by the query:

// Did we get back any rows?
if (@ mysql_num_rows($result) != 0) 
{

The function takes one parameter, the result set handle returned from mysql_query( ).

The last MySQL function called in showgifts( ) fetches the data:

// Fetch each database table row of the results
while($row = @ mysql_fetch_array($result))
{

This function retrieves row data, taking only the result set handle returned from mysql_query( ) as a parameter. Each call to mysql_fetch_array( ) fetches the next row of results and returns an array. In this example, the attributes are stored in the array $row. The function returns false when there are no more rows to fetch.

The attribute data stored in the array $row can be accessed associatively, that is, the attribute name can be used as a key to retrieve its value. For example, the following code prints the values of each presents table attribute as an HTML table row:

// Display the gift data as a table row
echo "\n<tr>" .
     "\n\t<td>{$row["quantity"]}</td>" .
     "\n\t<td>{$row["present"]}</td>" .
     "\n\t<td>{$row["colour"]}</td>" .
     "\n\t<td>{$row["shop"]}</td>" .
     "\n\t<td>{$row["price"]}</td>";

The name of the attribute from the presents table—for example, quantity—is used as an index in the statement {$row["quantity"]}. The braces are a new feature in PHP that allow all variables to be included directly into strings that are delimited by ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required