Replacing mysql_field_len() for schema length

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.

Historical function

<?php
// Historical only: removed from PHP 7.
$result = mysql_query('SELECT * FROM student');
$length = mysql_field_len($result, 1);
echo $length;
?>

Read declared length from INFORMATION_SCHEMA

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.

Quick inspection with SHOW COLUMNS

SHOW COLUMNS FROM student;

MySQLi result metadata

<?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()

← All PHP & MySQL topics




Subscribe to our YouTube Channel here



plus2net.com




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