Skip to content

Instantly share code, notes, and snippets.

@fethica
Forked from vishalkurup/json.php
Created July 30, 2014 09:21
Show Gist options
  • Save fethica/52e2f4f59aa4ed44f859 to your computer and use it in GitHub Desktop.
Save fethica/52e2f4f59aa4ed44f859 to your computer and use it in GitHub Desktop.
<?php
/***************************************************************
Description: City data in JSON.
Developer: Vishal Kurup
***************************************************************/
$host = "abc12345"; //Your database host server
$db = "abc12345"; //Your database name
$user = "abc12345"; //Your database user
$pass = "abc12345"; //Your password
$connection = mysql_connect($host, $user, $pass);
//Check to see if we can connect to the server
if(!$connection)
{
die("Database server connection failed.");
}
else
{
//Attempt to select the database
$dbconnect = mysql_select_db($db, $connection);
//Check to see if we could select the database
if(!$dbconnect)
{
die("Unable to connect to the specified database!");
}
else
{
$query = "SELECT * FROM cities";
$resultset = mysql_query($query, $connection);
$records = array();
//Loop through all our records and add them to our array
while($r = mysql_fetch_assoc($resultset))
{
$records[] = $r;
}
//Output the data as JSON
echo json_encode($records);
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment