Replacing mysql_field_type() for column types

mysql_field_type() returned a simplified field type from PHP's removed MySQL extension. For modern applications, use MySQL schema metadata when you need the table's declared type.

Historical mysql_field_type()

<?php
// Historical only.
$result = mysql_query('SELECT * FROM student');
$type = mysql_field_type($result, 0);
echo $type;
?>

Recommended: INFORMATION_SCHEMA

SELECT
    COLUMN_NAME,
    DATA_TYPE,
    COLUMN_TYPE,
    IS_NULLABLE,
    COLUMN_DEFAULT
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = 'sql_tutorial'
  AND TABLE_NAME = 'student'
ORDER BY ORDINAL_POSITION;

DATA_TYPE gives the base type, while COLUMN_TYPE retains additional type details.

PDO getColumnMeta()

<?php
$stmt = $dbo->query('SELECT COUNT(*) AS total FROM student');
$meta = $stmt->getColumnMeta(0);
print_r($meta);
?>

getColumnMeta() can be useful for result-set metadata, but schema queries are clearer when you specifically need the declared MySQL type.

MySQLi field type code

<?php
$result = $connection->query(
    'SELECT id, name, class, mark FROM student'
);
$field = $result->fetch_field_direct(0);
echo 'MySQL type code: ' . $field->type;
?>

For choosing the right schema type, continue with the MySQL data types hub and its numeric, date/time, and BLOB pages.

Data types · Column metadata · Field/schema length · Field names · Number of fields

← 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