English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The mysqli_stmt_error() function returns a description of the error that occurred
mysqli_stmt_error()The function returns a description of the error that occurred during the execution of the last statement.
mysqli_stmt_error($stmt)
Serial number | Parameters and descriptions |
---|---|
1 | stmt (required) This is an object representing the statement. |
The PHP mysqli_stmt_error() function returns a string value that describes the error that occurred during the execution of the last statement. If there is no error, this function returns an empty string.
This function was originally in PHP version5introduced and can be used in all higher versions.
The following example demonstratesmysqli_stmt_error()Function usage (procedural style)-
<?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("Create table.....\n"); mysqli_query($con, "INSERT INTO myplayers values(1, 'Sikhar', 'Dhawan', 'Delhi', 'India')"); print("Inserting record.....\n"); $stmt = mysqli_prepare($con, "SELECT * FROM myplayers"); mysqli_query($con, "DROP TABLE myplayers"); //Execute statement mysqli_stmt_execute($stmt); //Error $error = mysqli_stmt_error($stmt); print("Error : " . $error); //Conclusion mysqli_stmt_close($stmt); //Close connection mysqli_close($con); ?>
Output result
Creating table..... Inserting record..... Error: Table 'mydb.myplayers' doesn't exist
In the object-oriented style, the syntax of this function is$con->error. Here is an example of this function in an object-oriented style-
<?php //Establish connection $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("Create table.....\n"); $con -> query("INSERT INTO myplayers values(1, 'Sikhar', 'Dhawan', 'Delhi', 'India')"); print("Inserting record.....\n"); $stmt = $con ->prepare("SELECT * FROM myplayers"); $con ->query("DROP TABLE myplayers"); //Execute statement $stmt->execute(); //Error $error = $stmt ->error; print("Error: " . $error); //End statement $stmt->close(); //Close connection $con->close(); ?>
Output result
Creating table..... Inserting record..... Error: Table 'mydb.myplayers' doesn't exist
If the last executed statement object does not contain an error, this function returns an empty string-
<?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', 'Cape Town', 'South Africa'), (3, 'Kumara', 'Sangakkara', 'Matale', 'Sri Lanka')"; //Prepare statement $stmt = mysqli_prepare($con, '$query'); //Execute this statement mysqli_stmt_execute($stmt); print("Inserting record.....\n"); //Error $error = mysqli_stmt_error($stmt); print("Error : " . $error); //End statement mysqli_stmt_close($stmt); //Close connection mysqli_close($con); ?>
Output result
Creating table..... Inserting record..... Error :