English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

PHP Basic Tutorial

PHP Advanced Tutorial

PHP & MySQL

PHP Reference Manual

PHP mysqli_stmt_field_count() Function Usage and Example

PHP MySQLi Reference Manual

The mysqli_stmt_field_count() function returns the number of fields in the given statement.

Definition and usage

mysqli_stmt_field_count()The function accepts a statement object as a parameter and returns the number of fields in the result of the given statement.

Syntax

mysqli_stmt_field_count($stmt)

Parameter

Serial numberParameters and descriptions
1

stmt (required)

This is the object representing the statement for executing SQL queries.

Return value

The PHP mysqli_stmt_field_count() function returns an integer value indicating the number of rows in the result set returned by the statement.

PHP version

This function was initially introduced in PHP version5introduced and can be used in all higher versions.

Online example

The following examples demonstratemysqli_stmt_field_count()Usage of the function (procedural style), return field count:

<?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 records.....\n");
   //Retrieve the content of the table
   $stmt = mysqli_prepare($con, "SELECT * FROM myplayers");
   //Execute statement
   mysqli_stmt_execute($stmt);
   //Number of fields 
   $count = mysqli_stmt_field_count($stmt);
   print("Number of fields : ".$count);
   //End statement
   mysqli_stmt_close($stmt);
   //Close connection
   mysqli_close($con);
?>

Output results

Create table.....
Insert records.....
Number of fields : 5

Online example

In object-oriented style, the syntax of this function is$stmt->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 myplayers(ID INT, First_Name VARCHAR(255), Last_Name VARCHAR(255), Place_Of_Birth VARCHAR(255), Country VARCHAR(255))");
   print("Create table.....\n");
   $con ->query("INSERT INTO myplayers values(")1, 'Sikhar', 'Dhawan', 'Delhi', 'India')");
   $con ->query("INSERT INTO myplayers values(")2, 'Jonathan', 'Trott', 'CapeTown', 'SouthAfrica')");
   print("Insert records.....\n");
   //Retrieve data
   $stmt = $con ->prepare("SELECT First_Name, Last_Name, Country FROM myplayers");
   //Execute statement
   $stmt->execute();
   //Number of fields
   $count = $stmt->field_count;
   print("Number of fields: ".$count);
   //End statement
   $stmt->close();
   //Close connection
   $con->close();
?>

Output results

Create table.....
Insert records.....
Number of fields: 3

PHP MySQLi Reference Manual