目录

PHP mysqli sqlstate() 函数

❮ PHP mysqli 参考

示例 - 面向对象风格

返回最后一个错误的 SQLSTATE 错误代码:

<?php
$mysqli = new mysqli("localhost","my_user","my_password","my_db");

if ($mysqli -> connect_errno) {
  echo "Failed to connect to MySQL: " . $mysqli -> connect_error;
  exit();
}

// Table Persons already exists, so we should get an error
$sql = "CREATE TABLE Persons (Firstname VARCHAR(30), Lastname VARCHAR(30), Age INT)"
if (!$mysqli -> query($sql)) {
  echo "SQLSTATE error: ". $mysqli -> sqlstate;
}

$mysqli -> close();
?>

查看底部的程序样式示例。


定义和用法

sqlstate / mysqli_sqlstate() 函数返回最后一个错误的 SQLSTATE 错误代码。

错误代码由五个字符组成。 "00000" 表示没有错误。这些值由 ANSI SQL 和 ODBC 指定。


语法

面向对象风格:

$mysqli -> sqlstate

程序风格:

mysqli_sqlstate( connection)

参数值

Parameter Description
connection Required. Specifies the MySQL connection to use

技术细节

返回值: 包含最后一个错误的 SQLSTATE 错误代码的字符串
PHP 版本: 5+

示例 - 程序状态

返回最后一个错误的 SQLSTATE 错误代码:

<?php
$con=mysqli_connect("localhost","my_user","my_password","my_db");

// Check connection
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  exit;
}

// Table Persons already exists, so we should get an error
$sql = "CREATE TABLE Persons (Firstname VARCHAR(30), Lastname VARCHAR(30), Age INT)"
if (!mysqli_query($con, $sql)) {
  echo "SQLSTATE error: ". mysqli_sqlstate($con);
}

// Close connection
mysqli_close($con);
?>


❮ PHP mysqli 参考