// store the result in array form $result_set = array(); $i = 0; while ($i < $count) { $result_set[$i] = array(); $result_set[$i]['username'] = mysql_result($result, $i, 'username'); $result_set[$i]['first_name'] = mysql_result($result, $i, 'first_name'); $result_set[$i]['last_name'] = mysql_result($result, $i, 'last_name'); $i++; }
mysql_close();
echojson_encode($result_set);
// set the header to JSON header('Content-Type: application/json'); exit;
Add a new user
POST http://api.yoursite.com/user/add
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
<?php // only process it if it is POST request if ($_SERVER['REQUEST_METHOD'] === 'POST') { $sql = "INSERT INTO user VALUES ($_POST['username'], $_POST['first_name'], $_POST['last_name'])"; mysql_query($sql);
echojson_encode(array( 'message' => 'John Smith has been inserted' )); } else { // if not POST, then return 404 echojson_encode(array( 'message' => 'Endpoint not found' )); header('HTTP/1.1 404 Not Found'); }
// set the header to JSON header('Content-Type: application/json'); exit;
Client side
In this example will using this library - AFNetworking. It has been simplify a lot of code from iOS SDK (or just call it as a wrapper)