PHP MySQL Functions to get the length of data in a row of table
This old MySQL extension was deprecated in PHP 5.5.0 and was removed in PHP 7We can get the length of a field by using mysql_field_len function. This function gives the length of the field in the table structure. But this function does not give the length of actual data stored in the field. To get the length of data stored in a row of a table we have to use mysql_fetch_lengths function.
Use mysqli_fetch_lengths() in place of mysql_fetch_lengths().
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.
If we use mysql_fetch_lenghts within a while loop to display records by using mysql_fetch_row then for each row inside the loop we can display the length of the data stored in that row. For a single record if we are collecting by using mysql_fetch_object then we can use this to get the length of that row only. Here is the code to get the total length of data stored in a row.
In the above code the sum of data lengths of first row ( only ) is displayed. Here we have used the function array_sum to add all the elements of the array $nt2. The array variable $nt2 keeps all the data lengths of the fields of the row as elements of the array. Now let us try to display the sum of lengths of each field in a row. Here we will use mysql_fetch_row inside a while loop to display all the records of the table.
The above code will display the list for sum of the data length of each row.
To create student table used here, download sql dump
This article is written by plus2net.com team.
More on PHP MySQL functions