php-mysqlHow to get the error message for a MySQL query using PHP?
To get the error message for a MySQL query using PHP, you can use the mysqli_error()
function. This function takes the connection object as an argument and returns the error message.
Example code
$conn = mysqli_connect("localhost", "username", "password", "database");
$query = "SELECT * FROM table";
$result = mysqli_query($conn, $query);
if (!$result) {
echo mysqli_error($conn);
}
Output example
Table 'database.table' doesn't exist
Code explanation
mysqli_connect()
: Establishes a connection to a MySQL server. Takes four arguments: hostname, username, password, and database name.mysqli_query()
: Executes a query against the database. Takes two arguments: connection object and query string.mysqli_error()
: Returns the error message for the last query executed. Takes one argument: connection object.
Helpful links
More of Php Mysql
- How to get the last insert ID in PHP MySQL?
- How to use utf8mb4_unicode_ci in MySQL with PHP?
- How to export data from MySQL to Excel using PHP?
- How to create an SSL connection to MySQL using PHP?
- How to replace a string in MySQL using PHP?
- How to return multiple rows as an array in MySQL using PHP?
- How to escape a string for MySQL in PHP?
- How to list tables in PHP MySQL?
- How to get the version of MySQL using PHP?
- How to get the first row of a result in MySQL using PHP?
See more codes...