• PHP Video Tutorials

PHP mysqli_stmt_field_count() Function



Definition and Usage

The mysqli_stmt_field_count() 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)

Parameters

Sr.No Parameter & Description
1

stmt(Mandatory)

This is an object representing a statement executing an SQL query.

Return Values

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

PHP Version

This function was first introduced in PHP Version 5 and works works in all the later versions.

Example

Following example demonstrates the usage of the mysqli_stmt_field_count() function (in procedural style) −

<?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("Table Created.....\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("Record Inserted.....\n");

   //Retrieving the contents of the table
   $stmt = mysqli_prepare($con, "SELECT * FROM myplayers");

   //Executing the statement
   mysqli_stmt_execute($stmt);

   //Field Count 
   $count = mysqli_stmt_field_count($stmt);
   print("Field Count: ".$count);

   //Closing the statement
   mysqli_stmt_close($stmt);

   //Closing the connection
   mysqli_close($con);
?>

This will produce following result −

Table Created.....
Record Inserted.....
Field Count: 5

Example

In object oriented style the syntax of this function is $stmt->field_count; Following is the example of this function in object oriented style $minus;

<?php
   //Creating a 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("Table Created.....\n");
   $con -> query("INSERT INTO myplayers values(1, 'Sikhar', 'Dhawan', 'Delhi', 'India')");
   $con -> query("INSERT INTO myplayers values(2, 'Jonathan', 'Trott', 'CapeTown', 'SouthAfrica')");
   print("Records Inserted.....\n");

   //Retrieving Data
   $stmt = $con ->prepare("SELECT First_Name, Last_Name, Country FROM myplayers");

   //Executing the statement
   $stmt->execute();

   //Field Count
   $count = $stmt->field_count;
   print("Field Count: ".$count);

   //Closing the statement
   $stmt->close();

   //Closing the connection
   $con->close();
?>

This will produce following result −

Table Created.....
Records Inserted.....
Field Count: 3
php_function_reference.htm
Advertisements