0

All, If I run a query like the following:

 $qry = "Select wrong_column from table_name";
 $result = mysql_query($qry);

If wrong_column doesn't exist then I'll get a mySQL error. In PHP, how can I determine if there was an error from mySQL? If there was an error I'd like it to stop further processing but if there wasn't an error I'd like to get the mySQL results like this:

$resultset = mysql_fetch_array($result);

Would doing something like this work?

if(!mysql_error()){
    $resultset = mysql_fetch_array($result);
}

Any advice on how to do this would be appreciated. Thanks in advance!

2 Answers 2

4

how can I determine if there was an error from mySQL?

To see if an error occured you should test the result of mysql_query:

$result = mysql_query($qry);
if (!$result) {
    $error = mysql_error();
    // Handle the error.
} 

From the documentation:

For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error.

Emphasis mine.

Sign up to request clarification or add additional context in comments.

1 Comment

Would I want it to be !$result or leave it as !result?
-1

mysql_query("SELECT foo FROM bar") or exit(mysql_error()); is your best friend =)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.