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 use utf8mb4_unicode_ci in MySQL with PHP?
- How to join tables with PHP and MySQL?
- How to get the version of MySQL using PHP?
- How to write an update query in MySQL using PHP?
- How to count the number of resulting rows in a MySQL database 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?
- How to get the last insert ID in PHP MySQL?
- How to fetch data from MySQL in PHP?
See more codes...