PHP MySQL Functions to get the Field length from a result set

mysql_field_ln function will return the length of field associated with the field. This command takes one result set as input along with a field identifier or an offset. It returns a string holding field length details. Here is the command.
string mysql_field_len(int result, int field_offset)
Deprecated Removed in PHP 7 , Use these alternatives
Please note that the function mysql_field_len is not returning the length of the data stored in the field. It is giving the length of the field assigned in the structure of the table. So our data stored in that field can be less or equal the assigned length of that field. Check the end of this tutorial on how to get length of the data stored in a row.

We will try to develop one script using our student table to display the field length of all the fields.

$result = mysql_query ("SELECT * FROM student");
$no_of_fields = mysql_num_fields ($result);
$no_of_records = mysql_num_rows ($result);
$i = 0;
$table_name = mysql_field_table ($result, $i);
echo "Your '".$table_name."' table has ".$no_of_fields";
echo " fields and ".$no_of_records." records <BR>";
echo "The table has the following fields <BR>";
while ($i < $no_of_fields) {
// name of the field mysql_field_name
$name = mysql_field_name ($result, $i);
echo "Field name:<b>$name</b><br>";

// Field length  field mysql_field_len
$len = mysql_field_len ($result, $i);
echo "Field Length: $len<br>";

The above code will display the field type with the fields of the student table.

The result of the above script is here

Your ístudentí table has 4 fields and 52 records 
The table has the following fields
Field name:id
Field Length: 2

Field name:name
Field Length: 50

Field name:class
Field Length: 10

Field name:mark
Field Length: 12

As you have seen in the above code the length of the field is given but not the length of the data stored in the field. To get the length of data stored in a field we have to use mysqli_fetch_lengths() function.
PHP MySQL functions mysql_fetch_length() : Length of Data MySQL Error

Subscribe to our YouTube Channel here


* indicates required
Subscribe to plus2net

    Post your comments , suggestion , error , requirements etc here

    SQL Video Tutorials

    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 All rights reserved worldwide Privacy Policy Disclaimer