Skip to content

Instantly share code, notes, and snippets.

@johnbocook
Last active December 28, 2015 02:09
Show Gist options
  • Save johnbocook/7425352 to your computer and use it in GitHub Desktop.
Save johnbocook/7425352 to your computer and use it in GitHub Desktop.
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) . '}';
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment