0

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;
6
  • Use mysqli or PDO instead of mysql. Commented Dec 31, 2014 at 11:16
  • Have you checked $d? Commented Dec 31, 2014 at 11:17
  • $d is correct sorry you mean it can't be with mysql_?! Commented Dec 31, 2014 at 11:19
  • mysql is deprecated Commented Dec 31, 2014 at 11:21
  • first use mysql_fetch_assoc( $rank, $My_Connection); then debug using print_r(); then check your output. Commented Dec 31, 2014 at 11:23

4 Answers 4

1

$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

Sign up to request clarification or add additional context in comments.

1 Comment

i try this before but same result. i have another SELECT QUERY in this PHP file and that work
1
$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

Comments

0

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];

1 Comment

no this query give me id of that user this code work correctly on my server and my app work correctly. so answer is correct :D
0

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;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.