Name

mysql_fetch_lengths( )

Synopsis

mysql_fetch_lengths(results)

This returns an array containing the length of each field of a results set from a MySQL query.

...
$sql_stmnt = "SELECT wr_id, description, instructions
              FROM workreq";
$results = mysql_db_query('workrequests', $sql_stmnt);
while($row = mysql_fetch_object($results)) {
  $length = mysql_fetch_lengths($results);
  print "$row->wr_id: description: $length[1],
         instructions: $length[2] \n";
}
...

In this example, each work request number is selected, along with the brief description and the lengthy instructions. Looping through each row that is retrieved as an object with mysql_fetch_object() and a while statement, the code determines the length of the data for all three fields with mysql_fetch_lengths( ) and places them in an array. Within the statement block of the while statement, the value of the wr_id field is extracted, and the length of the description field and the instructions field is pulled out of the $length array using the relative index number for each. Here are a few lines from the output of this script:

...
5753: description: 26, instructions: 254
5754: description: 25, instructions: 156
5755: description: 25, instructions: 170

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.