English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The mysqli_num_fields() function returns the number of fields (columns) in the result set.
A PHP result object (mysqli_result class) represents the MySQL result returned by a SELECT or DESCRIBE or EXPLAIN query.
mysqli_num_fields()The function accepts a result object as a parameter, retrieves, and returns the number of fields (columns) in the given object.
mysqli_num_fields($result);
Serial number | Parameters and descriptions |
---|---|
1 | result (required) This is the identifier representing the result object. |
The PHP mysqli_num_fields() function returns an integer value specifying the number of fields in the given result object.
This function was originally introduced in PHP version5introduced and can be used in all higher versions.
The following examples demonstratemysqli_num_fields()Function usage (procedural style), the number of fields (columns) in the returned results:
<?php $con = mysqli_connect("localhost", "root", "password", "mydb"); mysqli_query($con, "CREATE TABLE myplayers(ID INT, First_Name VARCHAR(255), Last_Name VARCHAR(255), Place_Of_Birth VARCHAR(255), Country VARCHAR(255))"); print("Create table.....\n"); mysqli_query($con, "INSERT INTO myplayers values(1, 'Sikhar', 'Dhawan', 'Delhi', 'India')"); mysqli_query($con, "INSERT INTO myplayers values(2, 'Jonathan', 'Trott', 'CapeTown', 'SouthAfrica')"); mysqli_query($con, "INSERT INTO myplayers values(3, 'Kumara', 'Sangakkara', 'Matale', 'Srilanka')"); print("Insert record.....\n"); //Search table content $result = mysqli_query($con, "SELECT * FROM myplayers"); //Number of fields in the result $count = mysqli_num_fields($result); print("Number of fields in the result: ".$count); //End statement mysqli_free_result($result); //Close connection mysqli_close($con); ?>
Output result
Create table..... Insert record..... Number of fields in the result: 5
In the object-oriented style, the syntax of this function is$result-> field_count;.Here is an example of this function in an object-oriented style;
<?php //Establish connection $con = new mysqli("localhost", "root", "password", "mydb"); $con -> query("CREATE TABLE Test(Name VARCHAR(255), AGE INT)"); $con -> query("insert into Test values('Raju', 25),('Rahman', 30),('Sarmista', 27); print("Create table.....\n"); $stmt = $con -> prepare("SELECT * FROM Test WHERE Name in(?, ?)"); $stmt -> bind_param("ss", $name1, $name2); $name1 = 'Raju'; $name2 = 'Rahman'; //Execute statement $stmt->execute(); //Search results $result = $stmt->get_result(); //Number of fields $count = $result->field_count; print("Number of fields in the result: ".$count); //End statement $stmt->close(); //Close connection $con->close(); ?>
Output result
Create table..... Number of fields in the result: 2