每个人。
我正在使用通过 PHP 连接到 MySQL 的 iOS 应用程序。
我希望我的应用程序发送 PHP GET 请求并从 MySQL 数据库中获取一些数据,但我不知道如何将 MySQL 上的数据发送到 iOS。这是我的代码。
myDataGetter.m
//create a request and connect (url is my PHP file's)
NSURLRequest* getRequest = [NSURLRequest requestWithURL:url];
NSURLConnection* connection = [NSURLConnection connectionWithRequest:getRequest
delegate:self];
if (connection) {
//data received from PHP will be stored in receivedData
receivedData = [[NSMutableData alloc] init];
} else {
//error
}
在 myDataGetter.m 中,我设置了 NSURLConnection 的委托(delegate)方法。例如,
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
[receivedData setLength:0]
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[receivedData appendData:data];
}
在我的 PHP 文件中,
$con = mysql_connect ('host', 'user', 'pass');
mysql_select_db ('dbname', $con);
$rows = mysql_query ("SELECT name, age, email FROM table");
while ($row = mysql_fetch_array ($rows)) {
$name = $row["name"];
$age = $row["age"];
$email = $row["email"];
}
在此示例中,我想将 $name、$age 和 $email 发送到 iOS。如果您知道如何执行此操作,请帮助我。
谢谢。
最佳答案
对于这类工作,我总是使用 ASIHTTPRequest。它比 NSURLConnection 框架强大得多。
其次,我会使用 PHP 将结果编码为 JSON,然后使用适用于 iOS 的非常好的 SBJson 框架将请求的结果简单地放入 NSDictionary 或 NSMutableArray。
http://allseeing-i.com/ASIHTTPRequest/Setup-instructions
关于php - 通过 PHP 将 MySQL 记录发送到 iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10812650/