The old mysql_field_len() reported metadata about a field's defined length. Do not confuse a column's definition with the length of a value currently stored in a row.
<?php
// Historical only: removed from PHP 7.
$result = mysql_query('SELECT * FROM student');
$length = mysql_field_len($result, 1);
echo $length;
?>
SELECT
COLUMN_NAME,
DATA_TYPE,
CHARACTER_MAXIMUM_LENGTH,
CHARACTER_OCTET_LENGTH,
NUMERIC_PRECISION,
NUMERIC_SCALE
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = 'sql_tutorial'
AND TABLE_NAME = 'student'
ORDER BY ORDINAL_POSITION;
For character columns, CHARACTER_MAXIMUM_LENGTH is the declared maximum in characters, while CHARACTER_OCTET_LENGTH is the maximum in bytes.
SHOW COLUMNS FROM student;
<?php
$result = $connection->query(
'SELECT id, name, class, mark FROM student'
);
foreach ($result->fetch_fields() as $field) {
echo htmlspecialchars($field->name) . ': metadata length ' . $field->length . '<br>';
}
?>
If you need the length of the actual stored value, use current-row lengths or SQL functions such as LENGTH() and CHAR_LENGTH().
Current-row data lengths · Column metadata · Field types · Data types · String 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.