Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Catching mysql_query error

Tags:

php

mysql

I am trying to stop the mysql_query error from being output onto my form. Currently if there is no location found, I receive the error

"Warning: mysql_result() [function.mysql-result]: Unable to jump to row 0 on MySQL result index 11"

I am trying to catch this error, and instead assign the $location variable to not found. My code for attempting this is below, what am I doing wrong?

Thanks!

$query3 = "SELECT `location` FROM location WHERE vin = '$vin' LIMIT 1";
$result3 = mysql_query($query3);

if (!mysql_result($result3,0)) {
    $location = "Not found";
} else $location = mysql_result($result3,0,0);
like image 496
cds5059 Avatar asked Feb 16 '11 16:02

cds5059


1 Answers

mysql_result() generally shouldn't be used. You'd be better off with something like:

$result3 = mysql_query($query3) or die(mysql_error());

if (mysql_numrows($result3) == 0) then
   $location = "not found";
} else {
   $row = mysql_fetch_array($result3);
   $location = $row[0];
}

Your error is caused by the fact that the query returned no rows - e.g. nothing matched. You then tried to retrieve the first field in the first row of that result set, a row which doesn't exist. Checking the number of returned rows with mysql_numrows() is safer, as that works whether the query found nothing or a bajillion rows.

like image 187
Marc B Avatar answered Sep 30 '22 06:09

Marc B