Current Location: Home> Function Categories> mysqli::get_warnings

mysqli::get_warnings

(mysqli_get_warnings) Obtain the results of SHOW WARNINGS
Name:mysqli::get_warnings
Category:MySQLi
Programming Language:php
One-line Description:Get warning or error message related to the most recent execution statement

Function name: mysqli::get_warnings()

Applicable version: PHP 5.3.0 and above

Function description: The mysqli::get_warnings() method is used to obtain warnings or error messages related to the most recent execution statement. This method can only be called after the last query of the mysqli object connection to get warning information.

Syntax: mysqli::get_warnings()

Return value: Returns an instance of the mysqli_warning object, indicating the warning information in the query.

Sample code:

 // 创建mysqli对象并连接到数据库$mysqli = new mysqli("localhost", "username", "password", "database"); // 执行查询语句$result = $mysqli->query("SELECT * FROM table"); // 获取警告信息$warnings = $mysqli->get_warnings(); // 遍历并打印警告信息while ($warning = $warnings->fetch_object()) { echo "警告:".$warning->message."\n"; echo "错误码:".$warning->errno."\n"; echo "SQL状态:".$warning->sqlstate."\n"; } // 关闭数据库连接$mysqli->close();

Notes:

  1. Before calling the mysqli::get_warnings() method, a query statement must be executed first, otherwise the warning message will not be obtained.
  2. If the query does not generate any warning information, calling the mysqli::get_warnings() method will return NULL.
  3. The warning information may contain detailed information about warnings or errors about query execution, such as warning messages, error codes, and SQL status.
  4. Warning information is for the most recently executed statement. If there are multiple query statements, you can only get the warning information for the last query.
  5. After using the mysqli::get_warnings() method, be sure to call the mysqli_warning::free() method to free up memory to avoid memory leaks.
Similar Functions
Popular Articles