Name

mysql_fetch_row( )

Synopsis

mysql_fetch_row(results)

This returns an array containing a row of data from a results set given. This function is typically used in conjunction with a loop statement to retrieve each row of data in a results set. Each loop retrieves the next row. Individual fields appear in the array in the order they appeared in the SELECT statement, and can be retrieved by an array index. The loop ends when rows are used up, because the function returns NULL.

...
$sql_stmnt = "SELECT wr_id, client_name, description
              FROM workreq, clients
              WHERE workreq.clientid = clients.clientid";
$results = mysql_db_query('workrequests', $sql_stmnt);
while($row = mysql_fetch_row($results)) {
  print "WR-$row[0]: $row[1] - $row[2] \n";
}
...

To get the data for each element of the $row array created by mysql_fetch_row( ), the number corresponding to each element must be known. The index of the elements begins with 0, so $row[0] is the first element and, in this case, the work request number, because wr_id was the first field requested by the SELECT statement. Here’s one line of the output from the previous script:

WR-5755: Farber Investments - Can't connect to Internet.

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.