在cocoa-touch中通过PHP使用MYSQL数据?
我为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以将标签的文本设置为 row->cash: 的值
,或者您可能需要 NSString 类上的
stringWithCString:encoding:
类方法。you can set the text of the label to the value of row->cash:
or you might need the
stringWithCString:encoding:
class method on the NSString class.您应该让 PHP 脚本返回 JSON,或者只是返回您感兴趣的字符串。最好是 JSON。谷歌“REST API”,因为那是你真正想要的。
值得推荐的一些好的库是:
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:
Google will help you find those as well.