1

I'm trying to find how to read data from my database in my iphone app. The database is in my server, what I tried is similar to how I do that in Android (I'm not sure if is the best way):

NSString *hostStr = @"http://www.myip/notices.php";


NSData *dataURL =  [NSData dataWithContentsOfURL: [ NSURL URLWithString: hostStr ]];    
NSString *serverOutput = [[NSString alloc] initWithData:dataURL encoding: NSASCIIStringEncoding];

NSLog(@"Mysql response:is %@",serverOutput);

What I get is "Mysql response: is Array", what is true:

<?php
mysql_connect("localhost","user","pass");
mysql_select_db("database");


  $q = mysql_query("SELECT id, title FROM jos_content WHERE catid=12 AND state=1 AND DATE(publish_up) <= NOW() ORDER BY DATE(publish_up) DESC");

   while($e=mysql_fetch_assoc($q))

    $output[]=array("id" => $e['id'], "title" =>  utf8_encode($e['title']));
        echo $e;

print($output);

mysql_close();


?>

how should I do it?

Thank you in advance

user1256477
  • 10,763
  • 7
  • 38
  • 62

1 Answers1

2

From your php print the data as JSON it will be much easier to deal with it

In your PHP use

Instead of

print($output);

use

echo json_encode($output);

Then in your iOS app you could use this framewrok

Additional resources

To use SBJSon you would do the following

NSString *serverOutput = [[NSString alloc] initWithData:dataURL encoding: NSASCIIStringEncoding];
id object = [serverOutput JSONValue];
NSMutableDictionary *dictionary = (NSMutableDictionary *)object;

Now dictionary will contain the dictionary of values and keys

Community
  • 1
  • 1
Omar Abdelhafith
  • 21,163
  • 5
  • 52
  • 56
  • Thanks, I'm quite inexperienced. How can I use the framework? Thanks! – user1256477 Jun 27 '12 at 09:46
  • Thanks, but what I'm trying to do is add the framework to my project. https://github.com/stig/json-framework/blob/master/INSTALL.md but in the third step, where should I add the classes files? – user1256477 Jun 27 '12 at 09:57
  • new cuestion... NSLog(@"serverOutput %@", serverOutput); NSLog(@"Dictionary %@", dictionary); are the same, how it could be? – user1256477 Jun 27 '12 at 10:38
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/13103/discussion-between-user1256477-and-omar-abdelhafith) – user1256477 Jun 27 '12 at 10:40
  • @user1256477 sorry i had an error in my code, please check the updated answer – Omar Abdelhafith Jun 27 '12 at 10:57