I have the following query in my php file:

    if ($user) { //user is logged in (which he is)
        $highscore = mysql_query("SELECT highscore FROM highscore WHERE fbID = $user"); //wil give 200
        $fb_country_str = nl;

        if (mysql_num_rows($highscore) == 0) { //if no result from highscore set it to 1
            $highscore = 1;
        } else { //if we do get a result, select the country (in this case it is: uk)
            $country = mysql_query("SELECT country FROM highscore WHERE fbID = $user");
            if (!($country == $fb_country_str)) { //if the country is NOT the same, update the country with $fb_country_str (which in this case it should update)
                $sql = mysql_query("UPDATE highscore SET country = $fb_country_str WHERE fbID = $user");
            }
        }
    }

Now, when run, it doesn't update the database where country uk should be changed to nl. Why not? What am I missing?

Kind regards

link|improve this question

1  
Put your variables into ': $sql = mysql_query("UPDATE highscore SET country = '$fb_country_str' WHERE fbID = '$user'"); It will prevent the query errors, when country contains more than one word. – MarcinJuraszek 1 hour ago
1  
And even more important - you should think about using PDO php.net/manual/en/book.pdo.php – MarcinJuraszek 1 hour ago
1  
Please stop writing new code with the ancient mysql_* functions. They are no longer maintained and community has begun the deprecation process. Instead you should learn about prepared statements and use either PDO or MySQLi. If you cannot decide, this article will help to choose. If you care to learn, here is a good PDO-related tutorial. – Esailija 56 mins ago
I was not aware of this PDO stuff, will do some reading into it, thanks – Maurice 29 mins ago
feedback

4 Answers

up vote 1 down vote accepted

the mysql_query function doesn't return a string, it will return a resource that you can use with other mysql_* functions (see http://php.net/mysql_query)

So your variable $country is not what you expect. You need to use a function like mysql_fetch_assoc or mysql_result to get the country string

you can see samples on the php.net man page and also you used the return value as a resource (and not a scalar value) already in that script when you called the mysql_num_rows function on $highscore

link|improve this answer
Indeed I forgot the quotes (time for some coffee I guess)!!! Thanks – Maurice 30 mins ago
feedback
   if ($user) { //user is logged in (which he is)
        $highscore = mysql_query("SELECT highscore FROM highscore WHERE fbID = $user"); //wil give 200
        $fb_country_str = "nl"; //It's a string , you need quotes.

        if (mysql_num_rows($highscore) == 0) { //if no result from highscore set it to 1
            $highscore = 1;
        } else { //if we do get a result, select the country (in this case it is: uk)
            $getCountry = mysql_query("SELECT country FROM highscore WHERE fbID = $user");
//NOTICE - COUNTRY IS NOT THE COUNTRY VALUE YET!
$country = mysql_fetch_array($country);
$country = $country['country'];
            if (!($country == $fb_country_str)) { //if the country is NOT the same, update the country with $fb_country_str (which in this case it should update)
                $sql = mysql_query("UPDATE highscore SET country = $fb_country_str WHERE fbID = $user");
            }
        }
    }
link|improve this answer
feedback

$user - You a re treating it as a boolean (in the if statement) then a number (If I assume correctly) in the query

link|improve this answer
even if $user is number ... if condition will evaluate it false in case if($user) is if(0) – Rahul 41 mins ago
User is indeed a number, which is correct – Maurice 32 mins ago
feedback

Change your last if condition

if ($country != $fb_country_str) 
{ 
//if the country is NOT the same, update the country with $fb_country_str (which in this case it should update)
$sql = mysql_query("UPDATE highscore SET country = $fb_country_str WHERE fbID = $user");
}
link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.