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

时间:2011-07-13 22:22:14

标签: php iphone xcode

我将iphone应用程序中的变量“section”发布到php web服务。在检查数据库以进行数据检索之前,此Web检查是否设置了部分..

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

+(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();
}
?>

目标C API的哪个部分允许您捕获响应数据?如何修改上面的目标c函数来存储$ rows变量?

$ rows变量是一个存储来自数据库的元组的关联数组。

1 个答案:

答案 0 :(得分:2)

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

看一下URL Loading System Programming Guide - 它的例子非常全面。

相关问题