开发者

How to insert my UItextfield value to mysql web server in iphone application?

I m new in iphone development. friends i need to know how to send my UItextfield text value in mysql webserver? friends i reads many article related this topic but i could not get any proper information>

Friends some article is talking about php script all that ,

please send me proper code my friends

please othe开发者_如何学编程r wise my boss will fire me at my jobs

please help me

sorry for english....


The article you mentioned is right. One way is to use an intermediate PHP-Script. To use that approach install a script around that on your webserver:

<?php
// Connecting, selecting database
$link = mysql_connect('mysql_host', 'mysql_user', 'mysql_password')
    or die('Could not connect: ' . mysql_error());

mysql_select_db('my_database') or die('Could not select database');

// Performing SQL query
$query = 'INSERT INTO table SET column='.mysql_real_escape($_GET['value']);
$result = mysql_query($query) or die('Query failed: ' . mysql_error());

echo "OK";

// Free resultset
mysql_free_result($result);

// Closing connection
mysql_close($link);
?>

Then on iPhone you can:

// encode the textFields text to send it via GET
NSString* encodedValue = [textField.text
                          stringByAddingPercentEscapesUsingEncoding:
                          NSASCIIStringEncoding];

//construct an URL for your script, containing the encoded text for parameter value
NSURL* url = [NSURL urlWithString:
                    [NSString stringWithFormat:
                              @"http://yourServer.tld/script.php?value=%@",
                              encodedValue]];

NSError* error = nil;
//send the request synchronously, store the response in result
NSString* result = [NSString stringWithContentsOfURL:url
                             encoding:NSUFT8StringEncoding
                             error:&error];

if (!error && ([result isEqualToString:@"OK"])) {
    // everything went well
}

Although this should work, there's room for optimization:

On the PHP end: security checking, authorization

On the Cocoa end: asynchronous networking

Also you can find mysql-librarys for cocoa touch, but as this means you have to open up your MySQL Server to the web, this is normally not a recommended way to go.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜