Handle mysql_query() error without a variable

Posted by & filed under mySQL, PHP.

Thought this was a neat way of testing/handling query errors without assigning the result to a variable because you don’t need a variable unless you are using SELECT.

mysql_query($query);
if (mysql_error()) {
    throw new Exception(...);
}

mysql_error() returns empty string if there was no error, which evaluates to false, as pointed out by jakub-arnold in this post

Leave a Reply

  • (will not be published)

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Bitnami