Thursday, August 22, 2013

how to Fetch data from mysql as a json data?

how to Fetch data from mysql as a json data?

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.

No comments:

Post a Comment