Name

mysql_result( )

Synopsis

mysql_result(results, row[, field|offset])

This returns the data from one field of a row from results. Normally, this statement returns the next row and can be reused to retrieve results sequentially. As a third argument, either a field name (i.e., the column or alias name) or an offset may be given to change the pointer for the function. This function is typically used in conjunction with a loop statement to process each field of a results set.

...
$sql_stmnt = "SELECT client_name FROM clients";
$results = mysql_query($sql_stmnt);
$num_rows = mysql_num_rows($results);
for ($index = 0; $index < $num_rows; $index++) {
   print mysql_result($results, $index) . "\n";
}
...

This script queries the database for a list of client names. Using the mysql_num_row() function, the number of rows contained in the results set is determined. Using that bit of data, a for statement is constructed to loop through the results set using mysql_result( ) to extract one field of data per row. Otherwise, a function such as mysql_fetch_array( ) would have to be used in conjunction with the usual method of retrieving data from an array (e.g., $row[0]).

Get MySQL in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.