PHP和mysql,SELECT查询返回null

时间:2014-12-31 11:15:42

标签: php mysql database

这是PHP代码:

<?php
// header('Content-type: application/json');
header('Content-Type: text/html; charset=UTF-8');

$d=$_GET["userid"];

 $servername = "*******";
 $username = "***";
 $password = "****";
 $dbname = "*****";
 $arr = array();

  $My_Connection = mysql_connect ( $servername, $username , $password ) ;
     if ( ! $My_Connection )
       {
           die( ' Could not connect : ' . mysql_error( ) ) ;
       }
    //pick data base
    mysql_select_db ( $dbname, $My_Connection );

    mysql_set_charset('utf8',$My_Connection);

         $sql_tempcreate="CREATE TABLE tmp(id INT(11) UNSIGNED AUTO_INCREMENT PRIMARY KEY,DID VARCHAR(20) NOT NULL)";
         if(mysql_query($sql_tempcreate,$My_Connection))
         {
            $sql_convert="ALTER TABLE tmp CONVERT TO CHARACTER SET utf8";
            mysql_query($sql_convert);

           $sql_inserttotemp="INSERT INTO tmp (DID) SELECT DID FROM user WHERE 1 Order By HIGHSCORE DESC";
                if(mysql_query($sql_inserttotemp,$My_Connection))

                {
                 //***************************************** from here problem start
                    $sql_rank="SELECT * FROM `tmp` WHERE DID =".$d."";//    |
                    $r=mysql_query($sql_rank,$My_Connection);             //    |
                                                                          //    |
                    $rank= $r;                                            //    V
                }//******************************************************** to here

         }
         else
         {
            $rank= array("ERROR","ERROR");
         }
            $output = json_encode(array('top' => $arr,'rank' =>$rank));
            echo ($output);
    }
    else
    {
        echo "there is an error :(";
    }
    mysql_query("DROP TABLE tmp",$My_Connection);
mysql_close($My_Connection); 

?>

表tmp创建成功,数据插入到tmp表成功,但&#34;选择查询&#34;给我null! 实际上 $ r为空 我尝试(LIKE&amp; =)但结果相同

此查询有什么问题?

修改

我甚至将查询更改为:

$sql_rank="SELECT * FROM tmp WHERE DID=352136069213581" 

而不再工作:(

修改: 正确答案:

坦克 gaurav kumar 这是正确的代码:D

$sql_rank="SELECT * FROM `tmp` WHERE DID LIKE ".$d."";
$res=mysql_query($sql_rank,$My_Connection);
$r=mysql_fetch_assoc($res);
$rank= $r;

4 个答案:

答案 0 :(得分:1)

$d可能是一个字符串。所以你需要将它们用引号括起来(单引号或双引号) 同时修剪$d

的空格
$d=trim($_GET["userid"]);
$sql_rank="SELECT * FROM `tmp` WHERE DID ='".$d."'";

同时尝试使用mysqliPDO代替mysql,因为mysql_* functions are deprecated

答案 1 :(得分:1)

$d=trim($_GET["userid"]);
$sql_rank="SELECT * FROM `tmp` WHERE ID ='".$d."'";

您的代码如下:$d=$_GET["userid"];

$sql_rank="SELECT * FROM `tmp` WHERE DID ='".$d."'";

不是DID使用ID

答案 2 :(得分:0)

试试这个:

 $sql_rank="SELECT * FROM `tmp` WHERE DID =".$d."";
 $r=mysql_query($sql_rank,$My_Connection);
 $rank = mysql_fetch_row($r);

这将为您提供行数组,以便您可以像echo $rank[0];

一样进行提取

答案 3 :(得分:0)

坦克 gaurav kumar 这是正确的代码:D

$sql_rank="SELECT * FROM `tmp` WHERE DID LIKE ".$d."";
$res=mysql_query($sql_rank,$My_Connection);
$r=mysql_fetch_assoc($res);
$rank= $r;