如何从mysql中获取数据作为json数据? [重复]

I am using this code to fetch data from database and convert into json

    <?php
$username = "root";
$password = "";
$hostname = "localhost"; 

//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password , "test")
  or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";

//json 



$query = mysql_query("SELECT * FROM location");
$json_output = array();
while($row = mysql_fetch_assoc($query)){
    $json_output[] = json_encode($row);
}
echo json_encode($row);
?>

but iam getting the error as Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in C:\wamp\www\js.php on line 17

the line 17 is while($row = mysql_fetch_assoc($query)).

Pls some one help me in getting this.

</div>

Change the last lines to:

$query = "SELECT * FROM location";
$result = mysql_query($query);

while($row = mysql_fetch_assoc($result)){
    $json_output[] = $row;
}
echo json_encode($json_output);

BUT: You shouldn't use mysql* anyways, it's deprecated.