首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

mysqli_stmt::$sqlstate

(PHP 5, PHP 7)

mysqli_stmt :: $ sqlstate - mysqli_stmt_sqlstate - 从前面的语句操作返回SQLSTATE错误

描述

面向对象的风格

string $mysqli_stmt->sqlstate;

程序风格

代码语言:javascript
复制
string mysqli_stmt_sqlstate ( mysqli_stmt $stmt )

返回一个字符串,其中包含可以成功或失败的最近调用的预准备语句函数的SQLSTATE错误代码。错误代码由五个字符组成。'00000'表示没有错误。这些值由ANSI SQL和ODBC指定。有关可能值的列表,请参阅»http://dev.mysql.com/doc/mysql/en/error-handling.html

参数

代码语言:txt
复制
`stmt`   

仅过程风格:由mysqli_stmt_init()返回的语句标识符。

返回值

返回包含上次错误的SQLSTATE错误代码的字符串。错误代码由五个字符组成。'00000'表示没有错误。

注释

注意:并不是所有的MySQL错误都被映射到SQLSTATE的。值HY000(一般错误)用于未映射的错误。

例子

Example #1 Object oriented style

代码语言:javascript
复制
<?php
/* Open a connection */
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

$mysqli->query("CREATE TABLE myCountry LIKE Country");
$mysqli->query("INSERT INTO myCountry SELECT * FROM Country");


$query = "SELECT Name, Code FROM myCountry ORDER BY Name";
if ($stmt = $mysqli->prepare($query)) {

    /* drop table */
    $mysqli->query("DROP TABLE myCountry");

    /* execute query */
    $stmt->execute();

    printf("Error: %s.\n", $stmt->sqlstate);

    /* close statement */
    $stmt->close();
}

/* close connection */
$mysqli->close();
?>

Example #2 Procedural style

代码语言:javascript
复制
<?php
/* Open a connection */
$link = mysqli_connect("localhost", "my_user", "my_password", "world");

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

mysqli_query($link, "CREATE TABLE myCountry LIKE Country");
mysqli_query($link, "INSERT INTO myCountry SELECT * FROM Country");


$query = "SELECT Name, Code FROM myCountry ORDER BY Name";
if ($stmt = mysqli_prepare($link, $query)) {

    /* drop table */
    mysqli_query($link, "DROP TABLE myCountry");

    /* execute query */
    mysqli_stmt_execute($stmt);

    printf("Error: %s.\n", mysqli_stmt_sqlstate($stmt));

    /* close statement */
    mysqli_stmt_close($stmt);
}

/* close connection */
mysqli_close($link);
?>

上面的例子会输出:

代码语言:javascript
复制
Error: 42S02.

扫码关注腾讯云开发者

领取腾讯云代金券