PHP MySQLi 函数

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

语法

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

//面向过程风格
mysqli_get_warnings(mysql)

参数

mysql 必需。 仅适用于面向过程风格:指定 mysqli_connect() 或 mysqli_init() 返回的 mysqli 对象。

返回值

返回一个mysqli_warning对象。

示例:面向对象风格

下面的示例显示了mysqli::get_warnings()方法的用法。 p>

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

//执行SQL查询
$result = $mysqli->query("INSERT INTO Employee SET z = '1'");

//获取警告计数
$warning_count = $mysqli->warning_count;

//显示所有警告
if ($warning_count > 0) {
  $e = $mysqli->get_warnings();
  for ($i = 0; $i < $warning_count; $i++) {
    var_dump($e);
    $e->next();
  }
}

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

示例:面向过程风格

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

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

//执行SQL查询
$result = mysqli_query($mysqli, "INSERT INTO Employee SET z = '1'");

//获取警告计数
$warning_count = mysqli_warning_count($mysqli);

//显示所有警告
if ($warning_count > 0) {
  $e = mysqli_get_warnings($mysqli);
  for ($i = 0; $i < $warning_count; $i++) {
    var_dump($e);
    $e->next();
  }
}

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