php - 通过 PHP 将 MySQL 记录发送到 iOS

标签 php mysql ios http nsurlconnection

每个人。

我正在使用通过 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

http://stig.github.com/json-framework/

JSON encode MySQL results

关于php - 通过 PHP 将 MySQL 记录发送到 iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10812650/

相关文章:

php - Login.php 代码在登录过程中不检索电子邮件

ios - Apps Script vs Scripting Bridge v/s Custom AppleScript

javascript - Codeigniter 的 URL 问题(Azure AD 回复 URI)

php - FullCalendar - 如何根据日历上数据库(mysql)表行的逗号分隔日期显示事件

php - Linux 文件疯狂 - 获取日志文件最后一行的奇怪行为

php - 获取 4 个季度 mysql 的日期

mysql - Magento -- "SQLSTATE[23000]: Integrity constraint violation.."客户更新

ios - 无法验证您的应用程序错误

java - 如何在 MAC 上使用 Java 为 iPhone 自动化设置 Appium?

php - Laravel welcome.blade.php 无法正确加载