mysql_num_fields Functions to get number of fields returned in a query

WE can get the number of fields in our executed query by using mysql_num_fields function. This function returns the number of fields returned by the query.

Here is an example

$query="select * from student";
$result=mysql_query($query);
$fields=mysql_num_fields($result);
echo $fields;
The above code will print 4 as we have 4 columns in our student table.

Now let us change the query a bit ( only query part is changed and all other parts remain same )

$query="select name,id from student";
Now the output of the print command will display 2 ( not 4 ). So the function mysql_num_fields function returns the number of columns returned by the query.

Read how mysql_num_fields() is along with mysql_field_name function is used to display all column names of a table.



PHP MySQL functions mysql_num_rows() : Number of rows

Subhendu Mohapatra — author at plus2net
Subhendu Mohapatra

Author

🎥 Join me live on YouTube

Passionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.



Subscribe to our YouTube Channel here



plus2net.com
Not known

28-11-2009

The above code duplicates the value for each cell. Any fix?
smo

15-01-2010

Above code works perfectly. No problem.




SQL Video Tutorials










We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
©2000-2025   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer