I am trying to create an iOS application that, upon loading, will initially connect via HTTP back to the PHP web service, which will output data as JSON from the MySQL database. I would like it to import this data into a local SQLite database in an iOS application. I already downloaded the JSON-Framework for Objective-C.
My question is double.
1) What is the best way to output JSON from PHP so that I can send multiple database tables to the same JSON file? I have 4 data tables that I am trying to send (user, building, room, device). This is how I am currently outputting JSON data:
// Users $query = "SELECT * from user"; $result = mysql_query($query,$conn) or die('Errant query: '.$query); $users = array(); if(mysql_num_rows($result)) { while($user = mysql_fetch_assoc($result)) { $users[] = array('user'=>$user); } } // Buildings $query = "SELECT * from building"; $result = mysql_query($query,$conn) or die('Errant query: '.$query); $buildings = array(); if(mysql_num_rows($result)) { while($building = mysql_fetch_assoc($result)) { $buildings[] = array('building'=>$building); } } // Rooms $query = "SELECT * from room"; $result = mysql_query($query,$conn) or die('Errant query: '.$query); $rooms = array(); if(mysql_num_rows($result)) { while($room = mysql_fetch_assoc($result)) { $rooms[] = array('room'=>$room); } } // Devices $query = "SELECT * from device"; $result = mysql_query($query,$conn) or die('Errant query: '.$query); $devices = array(); if(mysql_num_rows($result)) { while($device = mysql_fetch_assoc($result)) { $devices[] = array('device'=>$device); } } header('Content-type: application/json'); echo json_encode(array('users'=>$users)); echo json_encode(array('buildings'=>$buildings)); echo json_encode(array('rooms'=>$rooms)); echo json_encode(array('devices'=>$devices));
I am afraid that this method is not the right way to send multiple objects.
2) In an iOS application, how can I automatically accept JSON data and insert it into the corresponding local database tables in SQLite?
Thanks for any help.
source share