The function mysqli_result::$field_count() returns the number of fields in the result set.
usage:
int mysqli_result::$field_count ( void )
Example:
// 连接到数据库$mysqli = new mysqli("localhost", "username", "password", "database"); if ($mysqli->connect_errno) { die("连接失败: " . $mysqli->connect_error); } // 执行查询语句$result = $mysqli->query("SELECT * FROM table"); // 获取结果集中的字段数量$fieldCount = $result->field_count; echo "结果集中的字段数量为: " . $fieldCount; // 释放结果集和关闭数据库连接$result->free(); $mysqli->close();
In the above example, we first use the mysqli class to connect to the database. We then execute a query statement and store the result in the $result variable. Next, we use the mysqli_result::$field_count() function to get the number of fields in the result set and store it in the $fieldCount variable. Finally, we output the number of fields.
Note that before using the mysqli_result::$field_count() function, you must execute the query statement and get the result set. At the same time, after completing the use of the result set, you should use the mysqli_result::free() method to release the result set and use the mysqli::close() method to close the database connection.