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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access