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.
<?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);
?>
<?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.
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.
<?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
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.
| 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. | |