Skip to content

Instantly share code, notes, and snippets.

@vishalkurup
Last active December 16, 2015 05:09
Show Gist options
  • Save vishalkurup/5382377 to your computer and use it in GitHub Desktop.
Save vishalkurup/5382377 to your computer and use it in GitHub Desktop.
PHP code to display data from our cities database in JSON format. This is the companion code to my iOS Tutorial on loading data from an external database into a table view on YouTube. Youtube channel: www.youtube.com/vtkurup/
<?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