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.
<?php
// Historical only.
$result = mysql_query('SELECT * FROM student');
$fields = mysql_num_fields($result);
echo $fields;
?>
<?php
$stmt = $dbo->query(
'SELECT id, name, class, mark FROM student'
);
echo $stmt->columnCount();
?>
<?php
$result = $connection->query(
'SELECT id, name, class, mark FROM student'
);
echo $result->field_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
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.