The mysqli_num_fields() function is used to display the number of fields in a result set. In other words, you can say that it returns the number of columns that is coming from a select query.
Syntax:
mysqli_num_fields(result);
In above syntax, there is only one parameter which is required parameter. It specify the result set. This function returns the number of fields in the result set.
For example:
<?php
$con=mysqli_connect("localhost","mysqlusername","mysqlpassword","dbname");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql="SELECT * FROM tablename";
if ($result=mysqli_query($con,$sql))
{
// Return the number of fields in result set
$fieldcount=mysqli_num_fields($result);
printf("Result set has %d fields.\n",$fieldcount);
// Free result set
mysqli_free_result($result);
}
mysqli_close($con);
?>
The above example return the number of fields in the result set returned by the function mysqli_query().
0 Comment(s)