mysqli_stmt_sqlstate()函數(shù)從上一個(gè)語句操作返回SQLSTATE錯(cuò)誤
mysqli_stmt_sqlstate()函數(shù)返回在執(zhí)行最后一條語句期間發(fā)生的SQLSTATE錯(cuò)誤。
mysqli_stmt_sqlstate($stmt);
序號 | 參數(shù)及說明 |
---|---|
1 | stmt(必需) 這是一個(gè)表示語句的對象(已準(zhǔn)備)。 |
PHP mysqli_stmt_sqlstate()函數(shù)返回一個(gè)字符串值,該字符串值表示在執(zhí)行最后一條語句期間發(fā)生的SQLSTATE錯(cuò)誤。如果沒有錯(cuò)誤,則此函數(shù)返回00000。
此函數(shù)最初是在PHP版本5中引入的,并且可以在所有更高版本中使用。
以下示例演示了mysqli_stmt_sqlstate()函數(shù)(面向過程風(fēng)格)的用法-
<?php $con = mysqli_connect("localhost", "root", "password", "mydb"); mysqli_query($con, "CREATE TABLE emp(ID INT, FirstName VARCHAR(20))"); print("創(chuàng)建表.....\n"); $stmt = mysqli_prepare($con, "INSERT INTO emp (ID) VALUES (?)"); mysqli_stmt_bind_param($stmt, "s", $id); $id = 'test'; //執(zhí)行語句 mysqli_stmt_execute($stmt); //State $state = mysqli_stmt_sqlstate($stmt); print("state: ".$state); //結(jié)束語句 mysqli_stmt_close($stmt); //關(guān)閉連接 mysqli_close($con); ?>
輸出結(jié)果
創(chuàng)建表..... state: HY000
在面向?qū)ο箫L(fēng)格中,此函數(shù)的語法為$con-> sqlstate。以下是面向?qū)ο箫L(fēng)格的此函數(shù)的示例-
<?php //建立連接 $con = new mysqli("localhost", "root", "password", "mydb"); $con -> query("CREATE TABLE myplayers(ID INT, First_Name VARCHAR(255), Last_Name VARCHAR(255), Place_Of_Birth VARCHAR(255), Country VARCHAR(255))"); print("創(chuàng)建表.....\n"); $con -> query("INSERT INTO myplayers values(1, 'Sikhar', 'Dhawan', 'Delhi', 'India')"); print("插入記錄.....\n"); $stmt = $con ->prepare("SELECT * FROM myplayers"); $con ->query("DROP TABLE myplayers"); //執(zhí)行語句 $stmt->execute(); //SQl State $state = $stmt ->sqlstate; print("SQL State: ".$state); //結(jié)束語句 $stmt->close(); //關(guān)閉連接 $con->close(); ?>
輸出結(jié)果
創(chuàng)建表..... 插入記錄..... SQL State: 42S02
以下是mysqli_stmt_sqlstate()函數(shù)的另一個(gè)示例-
<?php $con = mysqli_connect("localhost", "root", "password", "mydb"); mysqli_query($con, "CREATE TABLE emp(ID INT, FirstName VARCHAR(20), LastName VARCHAR(5), DateOfBirth VARCHAR(255), Salary INT)"); print("創(chuàng)建表.....\n"); $stmt = mysqli_prepare($con, "INSERT INTO emp values(?, ?, ?, ?, ?)"); mysqli_stmt_bind_param($stmt, "isssi", $id, $fname, $lname, $pob, $country); $id = 1; $fname = 'Swetha'; $lname = 'Yellapragada'; $dob = DATE('1981-12-05'); $country = 2366; //執(zhí)行語句 mysqli_stmt_execute($stmt); //Warnings $state = mysqli_stmt_sqlstate($stmt); print("state: ".$state); //結(jié)束語句 mysqli_stmt_close($stmt); //關(guān)閉連接 mysqli_close($con); ?>
輸出結(jié)果
創(chuàng)建表..... state: 22001