Name

mysql_fetch_object( )

Synopsis

mysql_fetch_object(result)

This returns a row of data as an object from the results set given. The function returns false if there are no more rows to return. The field names of the results set are used to retrieve data from the object returned.

...
$sql_stmnt = "SELECT count(wrid) AS wr_count, client_name
              FROM workreq, clients
              WHERE status <> 'done'
              AND workreq.clientid = clients.clientid
              GROUP BY workreq.clientid
              ORDER BY wr_count DESC";
$results = mysql_db_query('workrequests', $sql_stmnt);
while($row = mysql_fetch_object($results)) {
  print $row->client_name . " " . $row->wr_count . "\n";
}
...

This script is written to generate a list of clients that have outstanding work requests and to give a count of the number of requests for each, in descending order. Within the while statement that follows, each row of the results set is processed with mysql_fetch_object( ). The values of each element of the object created for each row is displayed by calls using the field names, not the column names. For instance, to get the data from the field with the number of work requests, the wr_count alias is used. Here are a few lines from the output of this script:

...
Bracey Logistics 3
Neumeyer Consultants  2
Farber Investments 4

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.