php - iPhone App Dev - 从 PHP 发送和接收数据

标签 php iphone xcode

我将一个变量“部分”从我的 iphone 应用程序发布到 php 网络服务。该网站在查询数据库以进行数据检索之前检查是否设置了部分。

我使用以下方法发布数据:

+(void)getQuestions:(NSInteger)sectionId from: (NSString*) url{
    //connect to database given by url
    NSMutableString* myRequestString = [[NSMutableString string]initWithFormat:@"section=%@", sectionId];
    NSData *myRequestData = [NSData dataWithBytes: [myRequestString UTF8String] length: [myRequestString length]];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString: url]]; 
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
    [request setHTTPMethod: @"POST"];
    //post section 
    [request setHTTPBody: myRequestData];
} 

在相同的方法中,我想捕获由 php 文件回显的数组:

<?php

//connect to database
function connect() {
  $dbh = mysql_connect ("localhost", "abc", "defgo") or die ('I cannot connect to the database because: ' . mysql_error());
  mysql_select_db("PDS", $dbh); 
  return $dbh;
}

//store posted data
if(isset($_POST['section'])){
  $dbh = connect();
  $section = $_POST['section'];
  $query = mysql_query("SELECT * FROM QUESTIONS WHERE sectionId = $section;") or die("Error: " . mysql_error());;

  $rows = array();
  while($r = mysql_fetch_assoc($query)) {
    $rows[] = $r;
  }      
  echo $rows;
  mysql_close();
}
?>

objective-c API 的哪一部分允许您捕获响应数据?如何修改上述 objective-c 函数以存储 $rows 变量?

$rows 变量是一个关联数组,用于存储数据库中的元组。

最佳答案

我在你的另一个问题中回答了这个问题,但我也会在这里回答。您可以使用 NSURLConnectionDelegate 来捕获响应,并在出现任何问题时提醒您。

看看 URL Loading System Programming Guide - 它的示例非常全面。

关于php - iPhone App Dev - 从 PHP 发送和接收数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6686411/

相关文章:

php - jQuery 到 PHP : Add a selector class to children elements if more than one

PHP 内爆值

c++ - 隐式模板参数

php - 如何禁用 <a href> 的功能

php - 日期时间 Paypal 返回

ios - 如何使用 FFMpeg for ios 创建 RTMP/RTSP 播放器

iphone - Xcode 6 启动屏幕尺寸如何/在哪里对应于 Apple 文档?

iphone - 如何通过 iOS 上的套接字下载和发布二进制数据?

ios - XIB 文件使用默认的 ViewController 类而不是文件所有者中设置的类

ios - 在每台设备上从相机胶卷中选择照片