THE WORLD'S LARGEST WEB DEVELOPER SITE

PHP mysqli_field_count() Function

❮ PHP MySQLi Reference

Example

Assume we have a "Friends" table that has 3 fields and 20 rows. Return the number of columns for the most recent query:

<?php
$con=mysqli_connect("localhost","my_user","my_password","my_db");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

mysqli_query($con,"SELECT * FROM Friends");
// Get number of columns - will always return 3
mysqli_field_count($con);

mysqli_close($con);
?>

Definition and Usage

The mysqli_field_count() function returns the number of columns for the most recent query.


Syntax

mysqli_field_count(connection);

Parameter Description
connection Required. Specifies the MySQL connection to use

Technical Details

Return Value: Returns an integer that represents the number of columns in the result set
PHP Version: 5+

❮ PHP MySQLi Reference