Replacing mysql_num_fields() for result column counts

mysql_num_fields() counted the number of columns in a result set using PHP's removed MySQL extension. Current PHP provides direct equivalents in PDO and MySQLi.

Historical mysql_num_fields()

<?php
// Historical only.
$result = mysql_query('SELECT * FROM student');
$fields = mysql_num_fields($result);
echo $fields;
?>

PDO columnCount()

<?php
$stmt = $dbo->query(
    'SELECT id, name, class, mark FROM student'
);
echo $stmt->columnCount();
?>

MySQLi field_count

<?php
$result = $connection->query(
    'SELECT id, name, class, mark FROM student'
);
echo $result->field_count;
?>

The SELECT list controls the count

SELECT name, id
FROM student;

The query above has two result columns even if the underlying table has many more. To count rows rather than columns, use row-count techniques or SQL COUNT().

Field names · Column metadata · Row counts · COUNT() · SELECT

← 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