user2203466
user2203466

Reputation: 31

PHP/MySQL - Error when using SELECT but no matching data

I want to get the ID of an image that is boostAmount-boostStart>total. Currently if an image exists that is appropriate it works. However, if there is nothing appropriate to show I get this error.

Invalid query: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ORDER BY RAND() LIMIT 1' at line 1

$photoToGuess = mysql_query("SELECT photoID,id,total,boostStart,boostAmount,auth,photoUploadDate FROM photos WHERE (boostAmount !=0) AND ((boostAmount-boostStart)>total) AND (auth=2 OR auth=5 OR auth=7) ORDER BY RAND() LIMIT 1") or die("Invalid query: " . mysql_error());
$getphotoToGuess = mysql_fetch_array($photoToGuess);

    //Yes
    if(mysql_num_rows($photoToGuess) > 0)
    {
    //do something
    }

Upvotes: 1

Views: 255

Answers (2)

riya
riya

Reputation: 116

Your if condition is wrong why are you using $photoToGuess in your if conditioner code here

if(mysql_num_rows($getphotoToGuess ) > 0){
    // do something
}

Upvotes: 1

RGI
RGI

Reputation: 161

Try this, I removed die condition you can achieve die condition using if statement...It is working fine chech once.

$photoToGuess = mysql_query("SELECT photoID,id,total,boostStart,boostAmount,auth,photoUploadDate FROM photos WHERE (boostAmount !=0) AND ((boostAmount-boostStart)>total) AND (auth=2 OR auth=5 OR auth=7) ORDER BY RAND() LIMIT 1");
$getphotoToGuess = mysql_fetch_array($photoToGuess);

//Yes
    if(mysql_num_rows($photoToGuess) > 0)
    {
    //do something
    }

You can refer this http://php.net/manual/en/function.mysql-query.php

Upvotes: 2

Related Questions