How to mysqli prepared statements error reporting in php?
All of the mysqli functions return false on failure, so you could easily just check for truthiness on each function and report errors.
1. Code:
<?php /* Open a connection */ $mysqli = new mysqli("localhost", "my_user", "my_password", "world"); /* check connection */ if (mysqli_connect_errno()) { printf("Connect failed: %s\n", mysqli_connect_error()); exit(); } $mysqli->query("CREATE TABLE myCountry LIKE Country"); $mysqli->query("INSERT INTO myCountry SELECT * FROM Country"); $query = "SELECT Name, Code FROM myCountry ORDER BY Name"; if ($stmt = $mysqli->prepare($query)) { /* drop table */ $mysqli->query("DROP TABLE myCountry"); /* execute query */ $stmt->execute(); printf("Error: %s.\n", $stmt->error); /* close statement */ $stmt->close(); } /* close connection */ $mysqli->close(); ?>
2. Definition and Usage
All of the mysqli functions return false on failure, so you could easily just check for truthiness on each function and report errors.The error / mysqli_error() function returns the last error description for the most recent function call, if any.
Syntax:
Object oriented style:
$mysqli -> error
Procedural style:
mysqli_error(connection)