开发者

Use MYSQL data via PHP in cocoa-touch?

I've created a working app for the iPhone which grabs data from a MYSQL database via a PHP page, then prints the data in a UIWebView.

But the issue here is that I no longer want to print the data in a UIWebView, I want to retrieve the data and pull it back into the app, to change a UILabel.text to the MYSQL data.

I currently have the following (which prints the data into a UIWebView (excluding most code due to DB connection passwords etc)):

PHP FILE:

    // Localize the GET variables
    $name   = isset($_GET['name']) ? $_GET['name']  : "";

    // Protect against sql injections
    $name  = mysql_real_escape_string($name);

    $cash = "SELECT cash FROM t_users WHERE username='$name' LIMIT 1";


    $result = mysql_query($cash,$conn);

    if(!$result) {
        die("Error retrieving scores " . mysql_error());
    }
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
         "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta name="viewport" content="width=320; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;"/>
    </head>
    <body>
        <?php
            // Display the table
           while ($row = mysql_fetch_object($开发者_JS百科result)) {
                echo ''.$row->cash.'';
        }

            mysql_free_result($result);
            mysql_close($conn);
        ?>
    </body>
</html>

PlayerInfo.m

-(IBAction)refreshData:(id)sender {

test1AppDelegate *mainDelegate = (test1AppDelegate *)[[UIApplication sharedApplication] delegate];

NSString * login_key = @"REMOVED FOR STACKOVERFLOW";
NSString * username = [mainDelegate.currentUser stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

NSString *urlString = [NSString stringWithFormat:@"REMOVED FOR STACKOVERFLOW/put_score.php?login_key=%@&name=%@",
                       login_key,username];
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[self.webview loadRequest:requestObj];
}

This changes shows the players CASH in the webview, but I want to change a UILabel to display the cash figure.

Please help?

Thank you!


you can set the text of the label to the value of row->cash:

label.text = [NSString stringWithUTF8String:row-cas];

or you might need the stringWithCString:encoding: class method on the NSString class.


You should have the PHP script return JSON or simply just return the string you're interested in. Preferably JSON. Google "REST API", because that's what you really want.

A few good libraries to recommend are:

  • JSONKit
  • ASIHTTPRequest

Google will help you find those as well.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜