php-mysqlHow to get query error message in PHP MySQL?
To get the query error message in PHP MySQL, you can use the mysqli_error()
function. This function returns the error message from the last MySQL operation.
Example code
$mysqli = new mysqli("localhost", "user", "password", "database");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$query = "SELECT * FROM table";
$result = $mysqli->query($query);
if (!$result) {
printf("Error: %s\n", mysqli_error($mysqli));
exit();
}
Output example
Error: Table 'database.table' doesn't exist
Code explanation
mysqli_error()
: This function returns the error message from the last MySQL operation.mysqli_connect_errno()
: This function returns the error code from the last MySQL operation.mysqli_connect_error()
: This function returns the error message from the last MySQL connection error.
Helpful links
More of Php Mysql
- How to insert a date into a MySQL database using PHP?
- How to output XML from MySQL using PHP?
- How to run multiple queries in PHP and MySQL?
- How to use a variable in a MySQL query using PHP?
- How to use a MySQL union in PHP?
- How to set a timeout for MySQL query in PHP?
- How to create an SSL connection to MySQL using PHP?
- What port to connect to MySQL from PHP?
- How to use the LIKE operator in PHP and MySQL?
- How to use utf8mb4_unicode_ci in MySQL with PHP?
See more codes...