Current Location: Home> Function Categories> mysqli_stmt::result_metadata

mysqli_stmt::result_metadata

(mysqli_stmt_result_metadata) Return result set metadata from the prepared statement
Name:mysqli_stmt::result_metadata
Category:MySQLi
Programming Language:php
One-line Description:Get the metadata of the result set after the preprocessing statement is executed

The mysqli_stmt::result_metadata() function is used to obtain the metadata of the result set after the preprocessing statement is executed.

Function usage: mysqli_stmt::result_metadata()

Example:

 <?php // 创建数据库连接$mysqli = new mysqli("localhost", "username", "password", "database"); // 检查连接是否成功if ($mysqli->connect_errno) { echo "连接数据库失败:" . $mysqli->connect_error; exit(); } // 准备预处理语句$stmt = $mysqli->prepare("SELECT id, name, age FROM users WHERE id > ?"); // 绑定参数$id = 10; $stmt->bind_param("i", $id); // 执行预处理语句$stmt->execute(); // 获取结果集的元数据$result_metadata = $stmt->result_metadata(); // 获取字段信息$fields = $result_metadata->fetch_fields(); // 打印字段信息foreach ($fields as $field) { echo "字段名: " . $field->name . "<br>"; echo "字段类型: " . $field->type . "<br>"; echo "字段长度: " . $field->length . "<br>"; echo "字段标志: " . $field->flags . "<br>"; echo "<br>"; } // 关闭预处理语句和数据库连接$stmt->close(); $mysqli->close(); ?>

In the example above, we first create a mysqli object to connect to the database. Then, we prepare a preprocessing statement with parameters and bind a parameter. Next, we execute the preprocessing statement and obtain the metadata of the result set. Then, we use the fetch_fields() method to get the field information and print out the field name, type, length and flag. Finally, the preprocessing statement and database connection are closed.

Please note that the mysqli_stmt::result_metadata() function is available in PHP 5.3.0 and above.

Similar Functions
Popular Articles