Youll be glad to know i got it working. I think this is what a webservice is, maybe ive been calling it something else.

Ive got the mySQL db sittin on a server in the cloud. There is a php read.php file and a write.php file.

The read php file takes the arguments for user and then reads the mySQL db and returns a json string which iphone reads and presents.

The write file takes the arguments input by the user and posts them to mySQL db.

So is this it? Now im only missing something that the server can use to update a 3rd updatedread.php file that will read the db every so often, get updated data and create an xml file that iphone can go and fetch whenever the user wants to get updated data…

Here is the write part:

$con = mysql_connect("localhost","usr","pass");
if (!$con)
die('Could not connect: ' . mysql_error());
mysql_select_db("iglobe", $con);
$sql="INSERT INTO users (name, udid) VALUES('$_POST[name]','$_POST[udid]')";
$sql="INSERT INTO  tags (originudid, latitude, longitude, destintyudid) VALUES
if (!mysql_query($sql,$con))
die('Error: ' . mysql_error());
echo "1 record added to tags";

And here is the read part:


$json = new Services_JSON();

$link = mysql_pconnect("localhost", "usr", "pass") or die("Could not connect");
mysql_select_db("iglobe") or die("Could not select database");

$query = "SELECT * FROM users";
$result = mysql_query($query);

$arr = array();
$rs = mysql_query("SELECT * FROM users");

while($obj = mysql_fetch_object($rs)) {
	$arr[] = $obj;

Echo $json->encode($arr);
//Echo '{:'.$json->encode($arr).'}';


And from your iphone you post like this:

+ (BOOL)updateTags:(NSString *)status{

    NSData *postData = [status dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];

    NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];

    NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@""]];
    [request setURL:url];
    [request setHTTPMethod:@"POST"];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [request setHTTPBody:postData];

    NSURLResponse *response;
    NSError *error;
    [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
    return (error == nil);


Leave a Reply