Mysql_query error hit

I am trying to stop mysql_query output from my form. Currently, if the location is not found, I get an error

"Warning: mysql_result () [function.mysql-result]: cannot go to row 0 in MySQL 11 result index"

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

Thank!

$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);
+5
source share
5 answers

mysql_result()usually should not be used. You will 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];
}

, - . . , . mysql_numrows() , , -.

+6

-, @:

if (!@mysql_result($result3,0)) {
    $location = "Not found";
} else $location = mysql_result($result3,0,0);

-, mysql_num_rows(result3) mysql_result.

0

, php ini - prod, .

http://www.php.net/manual/en/errorfunc.configuration.php#ini.error-reporting

, :

$result3 = mysql_query($query3);

This is the line where you should write if statements or "or die":

$result3 = mysql_query($query3)or die($location = "not found");
0
source

Mysql_query returns false if nothing is found so simply:

$result3 = mysql_query($query3);

if (mysql_affected_rows($result3) == 0) {
    $location = "Not found";
} else  $location = mysql_result($result3,0,0);

Must do it.

0
source

You should study the use of OOP; using a database class to interact with your database.

But basically you want to check if there are any rows before trying to return the results.

Try checking with "mysql_num_rows" in the if statement:

if (!mysql_num_rows($result3)) {
0
source

All Articles