返回最后一个错误的 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);
?>
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!