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

PHP Basic Tutorial

PHP Advanced Tutorial

PHP & MySQL

PHP Reference Manual

PHP mysqli_field_count() Function Usage and Example

PHP MySQLi Reference Manual

The mysqli_field_count() function returns the number of columns of the last query.

Definition and usage

mysqli_field_count()The function is used to get the number of fields (columns) in the result set of the last executed MySQL query.

Syntax

mysqli_field_count($con)

Parameter

Serial numberParameters and descriptions
1

con(required)

This is an object representing the connection to the MySQL Server.

Return value

The mysqli_field_count() function returns an integer value indicating the number of columns in the result set of the last query. If the last query is not a SELECT query (no result set), this function returns0.

PHP version

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

Online Example

The following examples demonstratemysqli_field_count()Usage of the function (procedural style)-

<?php
   //Establish connection
   $con = mysqli_connect("localhost", "root", "password", "mydb");
   //Query to retrieve all records from the employee table
   mysqli_query($con, "Select * from employee");
   //Field count
   $count = mysqli_field_count($con);
   print("Field count: " . $count);
   //Close connection
   mysqli_close($con);
?>

Output Result

Field count: 6

Online Example

In the object-oriented style, the syntax of this function is$con-> field_count;., where$conIs a connection object:

<?php
   //Establish connection
   $con = new mysqli("localhost", "root", "password", "mydb");
   //Query to retrieve all records from the employee table
   $con -> query("Select FIRST_NAME, LAST_NAME, AGE from employee");
   //Field count
   $count = $con-> field_count;
   print("Field count: " . $count);
   //Close connection
   $con -> close();
?>

Output Result

Field count: 3

Online Example

Below ismysqli_field_count()Another example of the function

<?php
   //Establish connection
   $con = mysqli_connect("localhost", "root", "password", "mydb");
   print("Field count: " . mysqli_field_count($con) . "\n");
   //Insert Query
   mysqli_query($con, "INSERT INTO employee (FIRST_NAME, AGE) VALUES (Archana, 25), (Bhuvan, 29);
   print("Field count: " . mysqli_field_count($con));
  
   //Close connection
   mysqli_close($con);
?>

Output Result

Field count: 0
Field count: 0

Online Example

Returns the number of columns of the last query:

<?php
   $connection_mysql = mysqli_connect("localhost", "root", "password", "mydb");
   
   if (mysqli_connect_errno($connection_mysql)){
      echo "Connection to MySQL failed: " . mysqli_connect_error();
   }
   
   mysqli_query($connection_mysql, "SELECT" * FROM employee);
   print(mysqli_field_count($connection_mysql));
   
   mysqli_close($connection_mysql);
?>

Output Result

6

PHP MySQLi Reference Manual