We now have a youtube channel. Subscribe!

PHP | mysqli_field_count() Function

PHP mysqli_field_count() Function


Hello folks! welcome back to a new edition of our tutorial on PHP. In this tutorial guide, we are going to be studying about the PHP mysqli_field_count() Function.

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

Syntax

Following below is the syntax to use this function -

mysqli_field_count($con)


Parameter Details

Sr.NoParameter & Description
1

con(Mandatory)

This is an object representing a connection to MySQL Server.


Return Value

This PHP function returns an integer value which indicates the number of columns in the result set of the last executed query. If the last query is not a SELECT query (no result set), this function returns 0.

PHP Version

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

Example1

The following example below illustrates the usage of built-in PHP mysqli_field_count() function (in a procedural style) -

<?php
   //Creating a connection
   $con = mysqli_connect("localhost", "root", "password", "mydb");

   //Query to retrieve all the records of the employee table
   mysqli_query($con, "Select * from employee");

   //Field Count
   $count = mysqli_field_count($con);
   print("Field Count: ".$count);

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

Output

When the above code is executed, it will produce the following result -

Field Count: 6

Example2

In an object oriented style the syntax of this function is $con->field_count;, where $con is the connection object -

<?php
   //Creating a connection
   $con = new mysqli("localhost", "root", "password", "mydb");

   //Query to retrieve all the records of the employee table
   $con -> query("Select FIRST_NAME, LAST_NAME, AGE from employee");

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

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

Output

When the above code is executed, it will produce the following result -

Field Count: 3

Example3

Following is another example of the built-in PHP mysqli_field_count() function -

<?php
   //Creating a 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 (Kennedy, 27), (Bethel, 27)");
   print("Field Count: ".mysqli_field_count($con));
  
   //Closing the connection
   mysqli_close($con);
?>

Output

When the above code is executed, it will produce the following result -

Field Count: 0
Field Count: 0

Example4

Try the following example below -

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

Output

When the above code is executed, it will produce the following result -

6


Alright guys! This is where we are rounding up for this tutorial post. In our next tutorial, we are going to be studying about the PHP mysqli_get_charset() Function.

Do feel free to ask your questions where necessary and we will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Do follow us on our various social media handles available and also subscribe to our newsletter to get our tutorial posts delivered directly to your emails.

Thanks for reading and bye for now.

Post a Comment

Hello dear readers! Please kindly try your best to make sure your comments comply with our comment policy guidelines. You can visit our comment policy page to view these guidelines which are clearly stated. Thank you.
© 2023 ‧ WebDesignTutorialz. All rights reserved. Developed by Jago Desain