PHP MySQLi 函数

PHP mysqli_stmt::get_warnings() / mysqli_stmt_get_warnings() 函数用于获取 SHOW WARNINGS 的结果。

语法

//面向对象风格
public mysqli_stmt::get_warnings()

//面向过程风格
mysqli_stmt_get_warnings(statement)

参数

statement 必需。 仅适用于面向过程风格:指定 mysqli_stmt_init() 返回的 mysqli_stmt 对象。

返回值

返回 mysqli_warning

示例:面向对象风格

下面的示例展示了mysqli_stmt::get_warnings()方法的用法。

<?php
//建立与数据库的连接
$mysqli = new mysqli("localhost", "user", "password", "database");
if ($mysqli->connect_errno) {
  echo "Failed to connect to MySQL: ". $mysqli->connect_error;
  exit();
}

//创建准备好的语句
$stmt = $mysqli->stmt_init();
$query = "INSERT INTO Employee SET z = '1'";
$stmt->prepare($query);

//执行SQL语句
$stmt->execute();

//收到警告
$warning = $stmt->get_warnings();;

//显示警告
echo "Warning: \n";
print_r($warning);

//关闭连接
$mysqli->close();
?>

上述代码的输出将类似于:

Warning:
mysqli_warning Object
(
    [message] => Unknown system variable 'z'
    [sqlstate] => HY000
    [errno] => 1193
)

示例:面向过程风格

下面的示例显示mysqli_stmt_get_warnings()的用法 函数。

<?php
//建立与数据库的连接
$mysqli = mysqli_connect("localhost", "user", "password", "database");
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: ". mysqli_connect_error();
  exit();
}

//创建准备好的语句
$stmt = mysqli_stmt_init($mysqli);
$query = "INSERT INTO Employee SET z = '1'";
mysqli_stmt_prepare($stmt, $query);

//执行SQL语句
mysqli_stmt_execute($stmt);

//收到警告
$warning = mysqli_stmt_get_warnings($stmt);;

//显示警告
echo "Warning: \n";
print_r($warning);

//关闭连接
mysqli_close($mysqli);
?>

上述代码的输出将类似于:

Warning:
mysqli_warning Object
(
    [message] => Unknown system variable 'z'
    [sqlstate] => HY000
    [errno] => 1193
)