user81993 user81993 - 1 year ago 65
SQL Question

How to figure out why a mysql query is failing?

In this case, I know why, I'm attempting to insert a row with a field data that is defined as unique in the table and it already exists. However, when I do mysqli::query, it just returns false. I want to display the reason to the user as there are multiple fail scenarios, how do I get the information on why the query failed?

Answer Source

You need to use mysqli::error

if (!$mysqli->query($query)) {
    printf("Errormessage: %s\n", $mysqli->error);

Or procedural style:

 echo mysqli_error($link);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download