PHP mysqli field_count() Function
Example - Object Oriented style
Assume we have a table named "Friends" (with 3 columns and 20 rows).
This example returns the number of columns for the most recent 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 number of columns - will return 3
$mysqli -> field_count();
$mysqli -> close();
?>
Look at example of procedural style at the bottom.
Definition and Usage
The field_count() / mysqli_field_count() function returns the number of columns for the most recent query.
Syntax
Object oriented style:
$mysqli -> field_count()
Procedural style:
mysqli_field_count(connection)
Parameter Values
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+ |
Example - Procedural style
Assume we have a table named "Friends" (with 3 columns and 20 rows).
This example returns the number of columns for the most recent 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 number of columns - will return 3
mysqli_field_count($con);
mysqli_close($con);
?>
❮ PHP MySQLi Reference
Copyright 1999-2023 by Refsnes Data. All Rights Reserved.