SQL PHP HTML ASP JavaScript articles and free scripts to download
 

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.

Download sql dump of student table


Discuss this tutorial at forum

List of SQL Tutorials


Scripts
PHP
JavaScript
SQL Tutorial List
SQL (Home)
mysql_affected_rows mysql_change_user mysql_close mysql_connect mysql_create_db mysql_data_seek mysql_db_name mysql_db_query mysql_drop_db mysql_errno mysql_error mysql_fetch_array mysql_fetch_assoc mysql_fetch_field mysql_fetch_lengths mysql_fetch_row mysql_field_flags mysql_field_len mysql_field_name mysql_field_seek mysql_field_table mysql_field_type mysql_free_result mysql_insert_id mysql_list_dbs mysql_list_fields mysql_list_tables mysql_num_fields mysql_num_rows mysql_pconnect mysql_query mysql_result mysql_select_db mysql_tablename
SQL site Map
Knowledge Management
Subscribe
Submit your email address and receive article and product notifications. Your email is safe with us.