json php mySQL iphone webService

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:

<?php
$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
('$_POST[originudid]','$_POST[latitude]','$_POST[longitude]','$_POST[receiver]')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "1 record added to tags";
mysql_close($con)
?>

And here is the read part:

<?php

include_once("JSON.php");
$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:@"http://www.domain.com/write.php"]];
    [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];
    NSLog(@"success!");
    return (error == nil);
}
Advertisements

10 thoughts on “json php mySQL iphone webService

  1. Hi, in your method “updateTags” what is “status”? I’m a newbie and trying to understand how you pass the values to the php write file.

    Thank you very much

    Marco.

    • Sorry about that….”status” is the value of the textfield passed in from the iphone app. In other words, whatever value you the user of the iphone app is entering into the app to be sent out to the cloud…

  2. I see your including a json.php file in your code. Where did you get that from? And is possible to get the code of your example? Did you get around to making a update php file?

    Good to see a simple example of how to make this work 🙂 Thx!

    • Yes Im now working on the final iPhone app. The Json files are available from many places online. I’ll look it up when I get to the office

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s