2011-07-13 101 views
3

我從我的iPhone應用程序發佈一個變量「節」到一個PHP的Web服務。此網頁查看是否部分isset詢問進行數據檢索數據庫之前..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(); 
} 
?> 

目標C API的哪些部分允許您捕獲響應數據?我如何修改上面的目標c函數來存儲$ rows變量?

的$行變量是一個關聯數組從數據庫中存儲的元組..

回答

1

我在您的其他問題回答了這一點,但我會在這裏回答它。您可以使用NSURLConnectionDelegate來捕獲響應,並提醒您任何問題。

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