0

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

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

亲切的问候

4

4 回答 4

1

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

所以你的变量$country不是你所期望的。您需要使用mysql_fetch_assocmysql_result之类的函数来获取国家/地区字符串

您可以在 php.net 手册页上看到示例,并且当您在$highscore上调用 mysql_num_rows 函数时,您已经在该脚本中使用了返回值作为资源(而不是标量值)

于 2012-05-26T11:09:47.110 回答
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");
            }
        }
    }
于 2012-05-26T11:09:27.367 回答
0

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

于 2012-05-26T11:10:00.473 回答
0

更改最后一个 if 条件

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");
}
于 2012-05-26T11:16:55.393 回答