Php/func mysqli sqlstate

来自菜鸟教程
跳转至:导航、​搜索

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)

参数值

参数 描述
connection 需要。指定要使用的MySQL连接

技术细节

返回值: 包含最后一个错误的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参考