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 a MySQL union in PHP?
- How to fetch data from MySQL in PHP?
- How to set a timeout for MySQL query in PHP?
- How to convert a MySQL timestamp to a datetime in PHP?
- How to list databases in PHP and MySQL?
- How to keep a connection open in PHP and MySQL?
- How to export data from MySQL to Excel using PHP?
- How to get table column names in PHP MySQL?
- How to join tables with PHP and MySQL?
- How to create an SSL connection to MySQL using PHP?
See more codes...