Current Location: Home> Function Categories> mysqli::$field_count

mysqli::$field_count

(mysqli_field_count) Returns the number of columns that have been recently queried
Name:mysqli::$field_count
Category:MySQLi
Programming Language:php
One-line Description:Returns the number of columns that were last query.

Definition and usage

field_count() / mysqli_field_count() function returns the number of columns in the last query.

Example

Example 1 - Object-Oriented Style

Suppose we have a table called "Friends" (with 3 columns and 20 rows).

This example returns the number of columns that were last query:

 <?php
$mysqli = new mysqli ( "localhost" , "my_user" , "my_password" , "my_db" ) ;

if ( $mysqli -> connect_errno ) {
  echo "Failed to connect to MySQL: " . $mysqli -> connect_error ;
  exit ( ) ;
}

$mysqli -> query ( "SELECT * FROM Friends" ) ;
// Get the number of columns - will return 3
$mysqli -> field_count ( ) ;

$mysqli -> close ( ) ;
?>

Example 2 - Procedural Style

Suppose we have a table called "Friends" (with 3 columns and 20 rows).

This example returns the number of columns that were last query:

 <?php
$con = mysqli_connect ( "localhost" , "my_user" , "my_password" , "my_db" ) ;

if ( mysqli_connect_errno ( ) ) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error ( ) ;
  exit ( ) ;
}

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

mysqli_close ( $con ) ;
?>
Similar Functions
Popular Articles