在cocoa-touch中通过PHP使用MYSQL数据?

发布于 2024-11-09 14:02:16 字数 2036 浏览 0 评论 0原文

我为 iPhone 创建了一个工作应用程序,它通过 PHP 页面从 MYSQL 数据库获取数据,然后在 UIWebView 中打印数据。

但这里的问题是我不再想在 UIWebView 中打印数据,我想检索数据并将其拉回到应用程序中,以将 UILabel.text 更改为 MYSQL 数据。

我目前有以下内容(将数据打印到 UIWebView 中(排除由于数据库连接密码等而导致的大多数代码)):

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($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];
}

此更改显示在 webview 中玩家现金,但我想更改 UILabel 来显示现金数字。

请帮忙?

谢谢你!

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($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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦毁影碎の 2024-11-16 14:02:16

您可以将标签的文本设置为 row->cash: 的值

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

,或者您可能需要 NSString 类上的 stringWithCString:encoding: 类方法。

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.

小霸王臭丫头 2024-11-16 14:02:16

您应该让 PHP 脚本返回 JSON,或者只是返回您感兴趣的字符串。最好是 JSON。谷歌“REST API”,因为那是你真正想要的。

值得推荐的一些好的库是:

  • JSONKit
  • ASIHTTPRequest

Google 也会帮助您找到这些库。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文