user559142
user559142

Reputation: 12517

iPhone App Dev - Send And Receive Data From PHP

I POST a variable "section" from my iphone app to a php web service. This web checks to see if section isset before interrogating the database for data retrieval..

I use the following method to post data:

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

Within the same method I would like to capture the array echoed by the php file:

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

What part of the objective C API allows you to capture response data? How can I modify the above objective c function to store the $rows variable?

The $rows variable is an associative array storing tuples from a database..

Upvotes: 4

Views: 1398

Answers (1)

Alex
Alex

Reputation: 8991

I answered this in your other question, but I'll answer it here as well. You can use NSURLConnectionDelegate to capture the response, and also alert you of any problems.

Take a look at the URL Loading System Programming Guide - it's quite comprehensive in its examples.

Upvotes: 2

Related Questions