tags:

views:

77

answers:

3

Hello, i have a problem with my iPhone application. I want to read a mysql database, one method is to serve the data over php. My php file looks like this:

 <?php


  $db_verbindung = mysql_connect("localhost", "root","") or die
    ("Keine Verbindung moeglich");
  $db = mysql_select_db("testdb") or die
    ("Die Datenbank existiert nicht.");

$querystring = $_POST['querystring'];
$result = mysql_query($querystring) or die(mysql_error()); 

$theString;

    while ($row = mysql_fetch_object($result)) { 
    $theString .=$row->productName."\n";
    }

  ?>

the other part in xcode:

NSString *post = @"querystring=select productID, productName from products";
NSData *postData = [post dataUsingEncoding:NSISOLatin1StringEncoding allowLossyConversion:NO];
NSMutableURLRequest *urlRequest = [[[NSMutableURLRequest alloc] init] autorelease];
[urlRequest setURL:[NSURL URLWithString:@"http://localhost/versionsnews/test.php"]];
[urlRequest setHTTPMethod:@"POST"]; 
[urlRequest setHTTPBody:postData];


NSData *urlData; 
NSURLResponse *response; 
NSError *error;
urlData = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:&error]; 
if(!urlData) {
    NSLog(@"Connection Failed!");
}



NSLog(@"Alles klar! empfangen:%d bytes",[urlData length]);
NSString *aStr = [[[NSString alloc] initWithData:urlData encoding:NSUTF8StringEncoding]autorelease]; 
NSLog(@"%@", aStr);

but the console only returns....

http://img683.imageshack.us/img683/7868/screenmq.png

A: 

Turn on the Breakpoints in Xcode to Build and Debug. Now it doesn't terminate but hangs and shows you where (and what) the bug is. Please post the response you get from the console.

Tim van Elsloo
A: 

Try printing out $theString at the bottom of your PHP script. You're building the variable but you're never outputting it!

Before "?>", say echo $theString;.

There may be other things going on; I stopped digging when I saw that.

Dan Ray
A: 

thanks Dan Ray!

my while loop now looks like this:

   while ($row = mysql_fetch_object($result)) { 
    echo $row->column1;
    echo $row->column2;
    }

but now i have a new problem: when i'm getting the data, it is one long string, beginning with 2 or 3 whitespaces... how can i fix this?

andi