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.
The field_count() / mysqli_field_count() function returns the number of columns for the most recent query.
$mysqli -> field_count()
mysqli_field_count(
connection)
Parameter | Description |
---|---|
connection | Required. Specifies the MySQL connection to use |
Return Value: | Returns an integer that represents the number of columns in the result set |
---|---|
PHP Version: | 5+ |
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);
?>
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!