user81993 user81993 - 7 months ago 11
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

You need to use mysqli::error

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

Or procedural style:

 echo mysqli_error($link);
Comments