PHP mysqli_sqlstate()Functions

PHP mysqli 参考手册PHP mysqli Reference Manual

Return the SQLSTATE error code for the last MySQL operation:

<?php 
// 假定数据库用户名:root,密码:123456,数据库:EXAMPLE 
$con=mysqli_connect("localhost","root","123456","EXAMPLE"); 
if (mysqli_connect_errno($con)) 
{ 
    echo "连接 MySQL 失败: " . mysqli_connect_error(); 
} 

// 数据表 websites 已存在,所以返回错误
$sql="CREATE TABLE websites (name VARCHAR(30),url VARCHAR(30),alexa INT)";
if (!mysqli_query($con,$sql))
{
    echo "SQLSTATE error: ". mysqli_sqlstate($con);
}

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

Definition and Usage

The mysqli_sqlstate() function returns the SQLSTATE error code for the last error.

The error code contains five characters. "00000" indicates no error. Values are specified by ANSI SQL and ODBC.


Syntax

mysqli_sqlstate(connection);

Parameters Description
connection Required. Specifies the MySQL connection to use.

Technical Details

Return value: Returns a string containing the SQLSTATE error code for the last error.
PHP version: 5+


PHP mysqli 参考手册PHP mysqli Reference Manual Other Extensions