mysql_fetch_lengths() Function in PHP

What is mysql_fetch_lengths() function in PHP?
How does mysql_fetch_lengths() works?

Explanation

This mysql_fetch_lengths() function returns the length of the contents of each field in a row.
Syntax
array mysql_fetch_lengths ( resource result)

This function returns a numeric array on success, or FALSE on failure or when there are no more rows.
This function mysql_fetch_lengths() returns an array and all elements of this array stores the length of data in each field of that row.
mysql_fetch_lengths() fetches the length of each field for the last row of data retrieved by mysql_fetch_array() , mysql_fetch_assoc() , mysql_fetch_object() , or mysql_fetch_row().
Example:

<?php
//Attempt to connect to the default database server
$link = mysql_connect("mysql_host", "mysql_user", "mysql_password")
or die ("Could not connect");
if (!mysql_select_db("my_database", $link)) {
echo " ERROR NO: " . mysql_errno($link) . "n";
}
// Simple Select query
$query = "SELECT * FROM my_table";
// Execute the query
$mysql_result = mysql_query($query,$link)
or die ("Query '$query' failed with error message: \"" . mysql_error () . '"');
// Grab the data from the result handle
$row = mysql_fetch_row($mysql_result);
// Grab the length of the data in the field
$length = mysql_fetch_lengths ($mysql_result);
echo"<table > <tr <td>Field Name</td><td>Field Value</td><td>Data Length</td></tr>";
for ( $i = 0; $i < 3; $i++ ) {
// Grab the field name from from the result handle
$name = mysql_field_name( $mysql_result, $i );
//Display field name, field value and Data length
echo"<tr><td>$name</td><td>$row[$i]</td><td>$length[$i]</td></tr>";
}
echo"</table>";
mysql_close($link);
?>

In the above example mysql_fetch_row() function grabs the first record from the result of the executed query. Mysql_field_name() function fetches the field name of result row returned by mysql_query() function. Mysql_fetch_lengths() function returns an array containing length of each field in an array

RESULT:
Field NameField ValueData Length
ID11
NAMEPHP3
PARENTID21

PHP Topics


Ask Questions

Ask Question