M2D
M2D

Reputation: 11

PHP and mysql, SELECT query return null

this is PHP code:

<?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); 

?>

table tmp create successfully , data insert to tmp table successfully but "select query" return me null! actually $r is null i try (LIKE & =)but same result

what is wrong with this query?

EDIT:

i even change the query to:

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

and not working again :(

tanks

EDIT: correct answer:

tanks to gaurav kumar this is correct code :D

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

Upvotes: 0

Views: 146

Answers (4)

M2D
M2D

Reputation: 11

tanks to gaurav kumar this is correct code :D

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

Upvotes: 0

AkshayP
AkshayP

Reputation: 2159

Try this:

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

This will give you array of rows so you can fetch like echo $rank[0];

Upvotes: 0

Alexander Gorgadze
Alexander Gorgadze

Reputation: 11

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

Your Code was like this : $d=$_GET["userid"];

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

Not DID use ID

Upvotes: 1

웃웃웃웃웃
웃웃웃웃웃

Reputation: 11984

$d may be a string.So you need to enclose them in quotes (single or doubble) Also trim spaces from $d

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

Also try using mysqli or PDO instead of mysql because mysql_* functions are deprecated

Upvotes: 1

Related Questions