PHP mysqli sqlstate() 函数
示例 - 面向对象的样式
返回最后一个 MySQL 操作的 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();
?>
$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)
参数值
参数 | 描述 |
---|---|
connection | 必需。规定要使用的 MySQL 连接。 |
技术细节
返回值: | 返回一个包含最后一个错误的 SQLSTATE 错误代码的字符串。 |
---|---|
PHP 版本: | 5+ |
Example - Procedural state
返回最后一个 MySQL 操作的 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);
?>
$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);
?>