编辑这个页面须要登录或更高权限!
mysqli_stmt_sqlstate()函数从上一个语句操作返回SQLSTATE错误
mysqli_stmt_sqlstate()函数返回在执行最后一条语句期间发生的SQLSTATE错误。
mysqli_stmt_sqlstate($stmt);
序号 | 参数及说明 |
---|---|
1 | stmt(必需) 这是一个表示语句的对象(已准备)。 |
PHP mysqli_stmt_sqlstate()函数返回一个字符串值,该字符串值表示在执行最后一条语句期间发生的SQLSTATE错误。如果没有错误,则此函数返回00000。
此函数最初是在PHP版本5中引入的,并且可以在所有更高版本中使用。
以下示例演示了mysqli_stmt_sqlstate()函数(面向过程风格)的用法-
<?php $con = mysqli_connect("localhost", "root", "password", "mydb"); mysqli_query($con, "CREATE TABLE emp(ID INT, FirstName VARCHAR(20))"); print("创建表.....\n"); $stmt = mysqli_prepare($con, "INSERT INTO emp (ID) VALUES (?)"); mysqli_stmt_bind_param($stmt, "s", $id); $id = 'test'; //执行语句 mysqli_stmt_execute($stmt); //State $state = mysqli_stmt_sqlstate($stmt); print("state: ".$state); //结束语句 mysqli_stmt_close($stmt); //关闭连接 mysqli_close($con); ?>
输出结果
创建表..... state: HY000
在面向对象风格中,此函数的语法为$con-> sqlstate。以下是面向对象风格的此函数的示例-
<?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("创建表.....\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"); //执行语句 $stmt->execute(); //SQl State $state = $stmt ->sqlstate; print("SQL State: ".$state); //结束语句 $stmt->close(); //关闭连接 $con->close(); ?>
输出结果
创建表..... 插入记录..... SQL State: 42S02
以下是mysqli_stmt_sqlstate()函数的另一个示例-
<?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("创建表.....\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; //执行语句 mysqli_stmt_execute($stmt); //Warnings $state = mysqli_stmt_sqlstate($stmt); print("state: ".$state); //结束语句 mysqli_stmt_close($stmt); //关闭连接 mysqli_close($con); ?>
输出结果
创建表..... state: 22001