mysqli_stmt_error()函數(shù)返回發(fā)生錯誤的描述
mysqli_stmt_error()函數(shù)返回在執(zhí)行最后一條語句期間發(fā)生的錯誤的描述。
mysqli_stmt_error($stmt)
序號 | 參數(shù)及說明 |
---|---|
1 | stmt(必需) 這是一個表示語句的對象。 |
PHP mysqli_stmt_error()函數(shù)返回一個字符串值,該字符串值表示對在執(zhí)行最后一條語句期間發(fā)生的錯誤的描述。如果沒有錯誤,則此函數(shù)返回一個空字符串。
此函數(shù)最初是在PHP版本5中引入的,并且可以在所有更高版本中使用。
以下示例演示了mysqli_stmt_error()函數(shù)的用法(面向過程風(fēng)格)-
<?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("創(chuàng)建表.....\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"); //執(zhí)行語句 mysqli_stmt_execute($stmt); //錯誤 $error = mysqli_stmt_error($stmt); print("Error : ".$error); //結(jié)束語 mysqli_stmt_close($stmt); //關(guān)閉連接 mysqli_close($con); ?>
輸出結(jié)果
創(chuàng)建表..... 插入記錄..... Error : Table 'mydb.myplayers' doesn't exist
在面向?qū)ο箫L(fēng)格中,此函數(shù)的語法為$con->error。以下是面向?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(); //Error $error = $stmt ->error; print("Error: ".$error); //結(jié)束語句 $stmt->close(); //關(guān)閉連接 $con->close(); ?>
輸出結(jié)果
創(chuàng)建表..... 插入記錄..... Error : Table 'mydb.myplayers' doesn't exist
如果最后執(zhí)行的語句對象中沒有錯誤,則此函數(shù)返回一個空字符串-
<?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')"; //預(yù)處理語句 $stmt = mysqli_prepare($con, $query); //執(zhí)行該語句 mysqli_stmt_execute($stmt); print("插入記錄.....\n"); //Error $error = mysqli_stmt_error($stmt); print("Error : ".$error); //結(jié)束語句 mysqli_stmt_close($stmt); //關(guān)閉連接 mysqli_close($con); ?>
輸出結(jié)果
創(chuàng)建表..... 插入記錄..... Error :