Function name: mysqli_stmt::get_warnings()
Applicable version: This function is suitable for PHP 5.3.0 and above.
Function description: The mysqli_stmt::get_warnings() method is used to obtain warning information related to the prepared statement of the last execution.
Usage example:
<?php // 创建数据库连接$mysqli = new mysqli("localhost", "username", "password", "database"); // 准备查询语句$stmt = $mysqli->prepare("SELECT * FROM users WHERE id = ?"); // 绑定参数$id = 1; $stmt->bind_param("i", $id); // 执行查询$stmt->execute(); // 获取警告信息$warnings = $stmt->get_warnings(); if ($warnings) { // 遍历警告信息并输出while ($warning = $warnings->fetch_assoc()) { echo "警告: " . $warning["message"] . "\n"; } } else { echo "没有警告信息。\n"; } // 关闭语句$stmt->close(); // 关闭数据库连接$mysqli->close(); ?>
In the above example, we first create a mysqli object, and then prepare a query statement using the prepare() method. Next, we bind a parameter and execute the query. Then, the warning information related to the statement is obtained by calling the get_warnings() method.
If a warning message exists, we use the fetch_assoc() method to traverse the result set of the warning message and output the message for each warning. If there is no warning message, "No warning message" is output.
Finally, we closed the statement and database connection.
Please note that before using the get_warnings() method, it must be called after executing the statement, otherwise the relevant warning information will not be obtained.