Name

mysql_fetch_assoc( )

Synopsis

mysql_fetch_assoc(results)

This returns an associative array containing a row of data from an SQL query results set. Field names of the results set are used as the keys for the values. Field names are derived from column names unless an alias is employed in the SQL statement. This function is typically used with a loop statement to work through a results set containing multiple rows of data. When there are no more rows to return, false is returned, which will end a loop statement. This function is synonymous with mysql_fetch_array( ) with MYSQL_ASSOC as its second argument.

...
$sql_stmnt = "SELECT wr_id, client_id, description
              FROM workreq";
$results = mysql_db_query('workrequests', $sql_stmnt);
while($row = mysql_fetch_assoc($results)) {
  print "WR-" . $row[wr_id] . ", Client-" .
         $row[client_id] . " " . $row[description] . "\n";
}
...

This loop is identical to the one for mysql_fetch_array( ) except that with the mysql_fetch_assoc( ) function, the index for a standard array could not be used to get the work request number—the wr_id key for the associative array stored in $row has to be used instead.

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.