English 中文(简体)
为什么我的查询没有更新?
原标题:Why isn t my query updating?
  • 时间: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");
            }
        }
    }

当运行时, 它不更新数据库, 国家 uk 应该在哪里被更改为 nl 。 为什么不呢? 我错过了什么?

谨请注意:

最佳回答

mysql_query 函数不返回字符串, 它会返回一个资源, 您可以用其它的 Mysql 函数使用( 见 < a href=" http://php. net/ mysql_query" rel= "nofolp" > http://php. net/ mysql_query )

所以您变量的“ 强” $ 国家 < / 强” 并不是您所期望的。 您需要使用“ 强” Mysql_ fetch_ assoc

您可以在php.net manp 页面上看到样本,您也可以使用返回值作为资源(而不是标量值),当您在 < 强'$highscore 上调 Mysql_ num_ rows 函数时,该脚本中已经使用返回值作为资源( 而不是标量值) 。

问题回答
   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");
            }
        }
    }

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




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签