johnbocook
11/12/2013 - 4:12 AM

Mysql query in PHP converted to json feed and displayed on page.

Mysql query in PHP converted to json feed and displayed on page.

<?php
//change these settings

$dbhost     = "";
$dbusername = "":
$dbpassword = "":
$dbname     = "";
$dbtable    = "";

//connect to database and grab the data
 
$connection = mysql_pconnect($dbhost, $dbusername, $dbpassword) or die("Could not connect");
mysql_select_db($dbname) or die("Could not find database");
$data = array();
$query = mysql_query("SELECT * FROM " . $dbtable);
while($row = mysql_fetch_object($query)) {
   $data[] = $row;
}

//output the data as a JSON feed

echo '{"JsonFeed":' . json_encode($data) . '}';
?>