When having an error in SQL syntax in classic PHP mysql, the query will not take place without any other effect. But in mysqli, it will kill the PHP script with Fatal error
mysql_query("SELECT title, misspelled_column FROM posts");
$mysqli->query("SELECT title, misspelled_column FROM posts");
In the first case, it will show the other queries and php output; but the second case kills the script by
Fatal error: Call to a member function fetch_assoc() on a non-object
The problem is related to non-object returned by false query. I can skip this error by
if($result){$row = $result->fetch_assoc();}
but my question is that why I did not need this check in classic mysql? With a more advanced system, one expects new features not missing what we had.
An error generated by MySQL should not be stopping execution. In fact, you can have your script show you any SQL errors by using
$mysqli->error(assuming$mysqliis your database connection, like in your example). However, what may be happening is that your mysqli error causes a particular object not to be created, and then calling a method on that object will create a fatal PHP error. For example:This will die not because you made an SQL error, but because
$stmtwas null and didn’t have the expectedexecute()method. So like everyone else has said, check your logs and see what the actual error is.Using
@to ignore errors is going to be hit-or-miss until you figure out which specific command is creating the error.update: If you know that the error is in the query, then you could check to see whether the query succeeded before you try to do anything with it. One way is to check the
errorparameter; another is to check to make sure that it actually returned the kind of object you want.Here are examples of both: