Function name: mysqli_stmt::$field_count()
Applicable version: PHP 5 >= 5.3.0, PHP 7
Function Description: This function is used to get the number of fields returned by the prepared statement.
usage:
mysqli_stmt::$field_count(): int
Parameters: None
Return value: Returns the number of fields returned by the prepared statement, and returns 0 if the statement does not return the result set.
Example:
// 创建数据库连接$mysqli = new mysqli("localhost", "username", "password", "database"); // 准备SQL语句$stmt = $mysqli->prepare("SELECT id, name, age FROM users WHERE age > ?"); // 绑定参数$age = 18; $stmt->bind_param("i", $age); // 执行查询$stmt->execute(); // 获取字段数量$fieldCount = $stmt->field_count; echo "返回的字段数量为: " . $fieldCount;
In the above example, we first create a mysqli object and establish a database connection. Then, we prepare a SELECT statement and bind a parameter using the bind_param() method. Next, we execute the query and use the $field_count property to get the number of fields returned by the prepared statement. Finally, we print out the number of fields.
Please note that this function can only get the correct number of fields after executing the mysqli_stmt::execute() method.