Replacing mysql_fetch_lengths() for row data lengths

The historical mysql_fetch_lengths() returned the byte length of each value in the most recently fetched row. This is different from the declared size of a column in the table definition.

Historical function

<?php
// Historical only: removed from PHP 7.
$q = mysql_query('SELECT * FROM student ORDER BY id');
$row = mysql_fetch_row($q);
$lengths = mysql_fetch_lengths($q);
echo array_sum($lengths);
?>

MySQLi lengths for the current row

<?php
$result = $connection->query(
    'SELECT id, name, class, mark FROM student ORDER BY id'
);
$row = $result->fetch_row();
$lengths = $result->lengths;
echo array_sum($lengths);
?>

MySQLi's result lengths property applies to the current fetched row. It reports byte lengths, so multibyte text can have more bytes than characters.

Ask SQL for character and byte lengths

SELECT
    id,
    name,
    CHAR_LENGTH(name) AS name_characters,
    OCTET_LENGTH(name) AS name_bytes
FROM student
ORDER BY id;

CHAR_LENGTH() counts characters; OCTET_LENGTH() counts bytes. This is usually the clearest approach when data length itself is part of the query.

PDO example

<?php
$stmt = $dbo->query(
    'SELECT id, name, CHAR_LENGTH(name) AS chars, OCTET_LENGTH(name) AS bytes
     FROM student ORDER BY id'
);
foreach ($stmt as $row) {
    echo htmlspecialchars($row['name']) . ': ' . $row['chars'] . ' chars, ' . $row['bytes'] . ' bytes<br>';
}
?>

For the declared column size rather than the current data value, see field length / schema length. The student table SQL dump is still available for practice.

Field/schema length · Column metadata · Field names · String length functions · Character length

← All PHP & MySQL topics




Subscribe to our YouTube Channel here



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.




SQL Video Tutorials










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