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 use a MySQL union in PHP?
- How to order by a column in MySQL using PHP?
- How to use a variable in a MySQL query using PHP?
- What port to connect to MySQL from PHP?
- How to insert a null value in MySQL using PHP?
- How to change database in MySQL with PHP?
- How to prepare a statement in MySQL using PHP?
- How to list databases in PHP and MySQL?
- How to insert a date into a MySQL database using PHP?
- How to check if a record exists in PHP and MySQL?
See more codes...