mysql_num_fields() function in PHP

What is mysql_num_fields() function in PHP?
How does mysql_num_fields() works?

Explanation

This mysql_num_fields() function in php gets number of fields in result set.
Syntax
int mysql_num_fields ( resource result)

Returns number of fields in result on success, or NULL on error.
mysql_num_fields() function returns the number of fields in the recordset returned by mysql_query operation.
Example

<?php
//Attempt to connect to the default database server
$link = mysql_connect("mysql_host", "mysql_user", "mysql_password")
or die ("Could not connect");
//select database
mysql_select_db("database", $link);
//execute simple query
$sql = mysql_query("SELECT * FROM table1", $link);
$result = mysql_query($sql,$link);
//Fetch number of fields in the result set
$fields=mysql_num_fields($result);
//print number of fields in the result set
echo"No of fields: ".$fields;
//Close connection
mysql_close($link);
?>

In the above code the result of mysql_query() result set is passed to mysql_num_fields() function to retrieve the count of fields present in the recordset returned by mysql_query function.
RESULT:
No of fields:12

See also:
mysql_select_db(), mysql_query(), mysql_fetch_field() and mysql_num_rows().

PHP Topics


Ask Questions

Ask Question