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 "
<tr>" .
     "
	<td>{$row["quantity"]}</td>" .
     "
	<td>{$row["present"]}</td>" .
     "
	<td>{$row["colour"]}</td>" .
     "
	<td>{$row["shop"]}</td>" .
     "
	<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 double quotation marks; if a variable can be unambiguously parsed from within a double-quoted string, the braces can be omitted.

Here’s an example of the output of the above code fragment:

<tr>
  <td>1</td>
  <td>Baileys Comet 6 Ladder</td>
  <td>Silver</td>
  <td>Bunnings</td>
  <td>97.50</td>

The code in showgifts( ) also uses associative array access to produce embedded links for each gift, such as:

<td><a href="action.php?action=insert&amp;present_id=10">Add to Shopping List</a>

In this example, when the user clicks the link, the script action.php is requested, and two parameters are passed: action=insert and present_id=10. In response to these parameters, the script action.php inserts the gift with the present_id of 10 into the shopping list of the guest who’s logged in. The script is discussed later in Section 11.6.

There are three tricks to accessing data returned from mysql_fetch_array( ) :

  • When both a table and attribute name are used in a SELECT statement, only the attribute name is used to access the data associatively. For example, after executing the statement:

    SELECT presents.quantity FROM presents

    the data is accessed associatively as $row["quantity"]. If two attributes have the same name, you must use aliases so that both can be accessed in the associative array. For example, the attributes in the following query:

    SELECT cust.name AS cname, stock.name AS sname FROM cust, stock

    can be accessed in an array as $row["cname"] and $row["sname"].

  • Aggregate functions such as SELECT count(*) FROM presents are associatively accessed as $row["count(*)"].

  • Prior to PHP 4.0.5, NULL values were not returned into the array. This doesn’t affect associative access but causes renumbering for numeric access. If a present has a color attribute that is NULL, the array that is returned has six elements instead of seven. The missing element can still be referenced as $row["color"] since referencing a nonexistent element returns NULL. However, if you want to avoid arrays of different lengths being returned, ensure that all attributes have a value or upgrade to a new release of PHP.

Other MySQL library functions can be used to process result sets differently. These are discussed in Chapter 18. However, all of the basic techniques needed to develop a simple application are shown by the functions in this chapter.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset