mysqli_stmt_error()函数返回发生错误的描述
mysqli_stmt_error()函数返回在执行最后一条语句期间发生的错误的描述。
mysqli_stmt_error($stmt)
序号 | 参数及说明 |
---|---|
1 | stmt(必需) 这是一个表示语句的对象。 |
PHP mysqli_stmt_error()函数返回一个字符串值,该字符串值表示对在执行最后一条语句期间发生的错误的描述。如果没有错误,则此函数返回一个空字符串。
此函数最初是在PHP版本5中引入的,并且可以在所有更高版本中使用。
以下示例演示了mysqli_stmt_error()函数的用法(面向过程风格)-
<?php $con = mysqli_connect("localhost", "root", "password", "mydb"); mysqli_query($con, "CREATE TABLE myplayers(ID INT, First_Name VARCHAR(255), Last_Name VARCHAR(255), Place_Of_Birth VARCHAR(255), Country VARCHAR(255))"); print("创建表.....\n"); mysqli_query($con, "INSERT INTO myplayers values(1, 'Sikhar', 'Dhawan', 'Delhi', 'India')"); print("插入记录.....\n"); $stmt = mysqli_prepare($con, "SELECT * FROM myplayers"); mysqli_query($con, "DROP TABLE myplayers"); //执行语句 mysqli_stmt_execute($stmt); //错误 $error = mysqli_stmt_error($stmt); print("Error : ".$error); //结束语 mysqli_stmt_close($stmt); //关闭连接 mysqli_close($con); ?>
输出结果
创建表..... 插入记录..... Error : Table 'mydb.myplayers' doesn't exist
在面向对象风格中,此函数的语法为$con->error。以下是面向对象风格的此函数的示例-
<?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(); //Error $error = $stmt ->error; print("Error: ".$error); //结束语句 $stmt->close(); //关闭连接 $con->close(); ?>
输出结果
创建表..... 插入记录..... Error : Table 'mydb.myplayers' doesn't exist
如果最后执行的语句对象中没有错误,则此函数返回一个空字符串-
<?php $con = mysqli_connect("localhost", "root", "password", "mydb"); mysqli_query($con, "CREATE TABLE myplayers(ID INT, First_Name VARCHAR(255), Last_Name VARCHAR(255), Place_Of_Birth VARCHAR(255), Country VARCHAR(255))"); print("Table Created.....\n"); $query = "INSERT INTO myplayers values(1, 'Sikhar', 'Dhawan', 'Delhi', 'India'),(2, 'Jonathan', 'Trott', 'CapeTown', 'SouthAfrica'),(3, 'Kumara', 'Sangakkara', 'Matale', 'Srilanka')"; //预处理语句 $stmt = mysqli_prepare($con, $query); //执行该语句 mysqli_stmt_execute($stmt); print("插入记录.....\n"); //Error $error = mysqli_stmt_error($stmt); print("Error : ".$error); //结束语句 mysqli_stmt_close($stmt); //关闭连接 mysqli_close($con); ?>
输出结果
创建表..... 插入记录..... Error :