PHP MySQL Functions to get the length of data in a row of table

We 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.

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 function gives the array with the lengths of the current row. So this has to be used after collecting or fetching a record from a table. There are many functions used in different condition to collect records one at a time or a group of records at a time by using functions like mysql_fetch_row, mysql_fetch_array and mysql_fetch_object.

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.

$q=mysql_query("select * from student order  by  id ");
$nt=mysql_fetch_object($q);
$nt2=mysql_fetch_lengths($q);
echo array_sum($nt2);
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.

$q=mysql_query("select * from student order  by  id ");
echo "<table>";

while($nt=mysql_fetch_row($q)){
$nt2=mysql_fetch_lengths($q);
echo "<tr >";
while (list ($key, $val) = each ($nt)){echo "<td>$val</td>";}
echo "<td>".array_sum($nt2)."</td>";
echo "</tr>";
}
echo "</table>";


The above code will display the list for sum of the data length of each row.



PHP MySQL functions mysql_field_flags() mysql_field_name() : Name of the field

Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com
    ashish

    28-02-2013

    what i want is that suppose there is 3 row and 1 coloum and in 3 rows the value is 1,2,3 so i want add this and get 6 as the row increase the total value which is right now 6 get automatically updated.

    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 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer