为什么我的查询不更新?

时间:2012-05-26 11:06:08

标签: php mysql

我的php文件中有以下查询:

    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");
            }
        }
    }

现在,在运行时,它不会更新国家/地区应更改为nl的数据库。为什么不?我错过了什么?

亲切的问候

4 个答案:

答案 0 :(得分:1)

mysql_query函数不返回字符串,它将返回一个可以与其他mysql_ *函数一起使用的资源(参见http://php.net/mysql_query

因此您的变量 $ country 不是您所期望的。您需要使用 mysql_fetch_assoc mysql_result 等函数来获取国家/地区字符串

你可以在php.net手册页上看到样本,当你在 $ highscore

答案 1 :(得分:0)

   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");
            }
        }
    }

答案 2 :(得分:0)

$ user - 您将其视为布尔值(在if语句中),然后在查询中使用数字(如果我假设正确)

答案 3 :(得分:0)

更改您的上一个条件

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");
}