1

Following code gives no error message, there is absolute no output. I have tried to run query in phpmyadmin, it is working perfectly.

<?php
         $db = mysql_connect(178.62.64.68, user1254, XXXX) or die('Could not connect: ' . mysql_error());
         mysql_select_db(db001) or die('Could not select database');

        $query = "SELECT ID, DATE, SCORE FROM table001 ORDER by SCORE asc LIMIT 10";
        $result = mysql_query($query) or die('Query failed: ' . mysql_error());

       $result_array = array();  
       while($row = mysql_fetch_array($result))
        {
           $result_array[] = array(
              'id' => $row['ID'],
              'score' => $row['SCORE'],
              'date' => $row['DATE']
           );
        }        
       echo json_encode($result_array);
?>

1 Answers1

4

You are missing the quotes around your arguments passed to the functions. The first two lines must be as so:

 $db = mysql_connect('178.62.64.68', 'user1254', 'XXXX') or die('Could not connect: ' . mysql_error()); // note the quotes (could have been "")
 mysql_select_db('db001') or die('Could not select database'); // note the quotes (could have been "")

string will be treated as a CONSTANT.

'string' (or "string") will be evaluated as a string (which is what we want here).

Finally, stop using mysql_* functions, they are deprecated. Start using MySQLi or PDO instead.

Community
  • 1
  • 1
D4V1D
  • 5,805
  • 3
  • 30
  • 65