I have a mySQL db read by php page which puts the data into an array and encodes it as an object which is then read by an iOS app as an object and converted to an array again.

this scenario pulls data from a database and returns the data in this format:
[ {key1:value1, key2:value2...}, {keyA:valueA, keyB:valueB...}, ... ]
a comma separates each data piece in the array

I want to do the same but with a dictionary. I want the php to read the mySQL and then encode it as a dictionary object to pass to the iOS. how can i do that? I will want to say; get the key:value set where key1:value1 = "xyz"...so ill have to get all its values as a dictionary like so:

[ {key1:value1, key2:value2, key3:value3, key4:value4.......} ]

how do i do this?

    i just realized the TwitterHelper class file uses this format:

    @"http://www.twitter.com/statuses/user_timeline/%@.json"

    where the %@ is the username passed to it...so does this mean there is a special webpage created for every user when the request is made? Does this mean i would need a special server to server .json pages created dynamically from my php-mysql code?

      marciokoko;10964035 wrote:

      where the %@ is the username passed to it...so does this mean there is a special webpage created for every user when the request is made?

      Well, one page for each user - yes. Several script files - no. Just like any other script used to create dynamic output, no page is the same as the other, even though the script producing them is.
      They are most likely using one single script and url rewrites to prettify the url.

      marciokoko;10964035 wrote:

      Does this mean i would need a special server to server .json pages created dynamically from my php-mysql code?

      No. There is nothing magic about it. You just turn an array into a string according to a specific format: i.e. the way javascript would interpret the string as an array if evaluating the string. Also, someone else has done the job for you.

      $arr = somehowCreateArray($data);
      echo json_encode($arr);
      

        Ok, so currently I have a php that reads the db and creates an array object using mysql_fetch_object and at the end it encodes it as json using echo $json->encode(array);

        I load that page and get the correct json format on screen. This page is called readtags.php. So do you mean I could just have this page take an input variable "username" and output a file called username.json which contains simply the echoed encoded json file for a specific user?

        If so, I would have to eliminate each username.json page immediately after use so the pages wouldn't keep building up on the server?

          marciokoko;10964079 wrote:

          So do you mean I could just have this page take an input variable "username"

          Yes.

          marciokoko;10964079 wrote:

          and output a file called username.json which contains simply the echoed encoded json file for a specific user?

          I may be missing the point here since I know nothing about twitter, I barely know what it is. Are you really saying that twitter for some reason needs such a file on your server?
          If they do and you get a lot of such requests and building the response takes too much time for your server to handle, keeping static username.json files would be far more efficient than building them each time. If this is the case, then yes.
          Otherwise I'd still make sure I managed to deal with incoming requests by using the one script "readtags.php" mentioned above with a username parameter and just echo the output as you allready do.

            ok these are excerpts from the twitter class that you can use in the iOS app to access its API which is REST i think:

            they post to their data stores:

            @"http://%@/statuses/user_timeline/%@.json"
            @"status=%@"
            @"http://%@:%@@%@/statuses/update.json"

            which are all via HTTP POST

            and they read the data:

            @"http://%@/users/show/%@.json"
            @"http://%@/statuses/user_timeline/%@.json"

            Thats why im thinking they create user.json files, which would be inefficient because once that file is created it must then be erased, so it would just be easier to make a generic readtags.php script and have it spit out the data, which is where im at right now. so i could just say:

            http://www.server.com/app/readtag.php?user=phpbuilder and have the php file incorporate the variable into its query and spit out the correct json enocded data, right?

            One more thing, this is the code im currently using:

            <?php

            include_once("JSON.php");
            $json = new Services_JSON();

            $link = mysql_pconnect("localhost", "user", "paas") or die("Could not connect");
            mysql_select_db("app") or die("Could not select database");

            $query = "SELECT * FROM tags";

            $arr = array();
            $rs = mysql_query("SELECT * FROM users");

            while($obj = mysql_fetch_object($rs)) {
            $arr[] = $obj;
            }
            Echo $json->encode($arr);
            ?>

            which you can see just spits out the entire users table in this format:
            [
            {"id":"1","name":"user","pass":"","udid":"269d40ba 6575b9ec51a7f3237e757c2bcd6bf6","timestamp":"0000-00-00 00:00:00","reglocation":""},
            {"id":"2","name":"other","pass":"","udid":"2031aaf b778b3a27635ae38e4315f31bba956805","timestamp":"00 00-00-00 00:00:00","reglocation":""},
            {"id":"4","name":"hers","pass":"","udid":"F6869 FB4-2EBE-5D43-A62D-5D4007646764","timestamp":"2010-09-18 11:15:16","reglocation":""},
            {"id":"5","name":"his","pass":"","udid":"adf03902c 18974c9d29edf27da779afa66438a3b","timestamp":"2010-09-18 11:18:52","reglocation":""}
            ]

            which is an array of dictionaries. Once i pass it the specific name, such as getusers.php?user=hers then it will only spit out:

            {"id":"4","name":"hers","pass":"","udid":"F6869 FB4-2EBE-5D43-A62D-5D4007646764","timestamp":"2010-09-18 11:15:16","reglocation":""}

            which is a dictionary, correct? Then i get the objectForKey:name and get its value, add it to an array and use it to populate a VC....

              marciokoko;10964093 wrote:

              and they read the data:

              @"http://%@/users/show/%@.json"
              @"http://%@/statuses/user_timeline/%@.json"

              Thats why im thinking they create user.json files, which would be inefficient because once that file is created it must then be erased

              Why does such a file have to be erased? If you need to update it, you just overwrite the old file.

              marciokoko;10964093 wrote:

              , so it would just be easier to make a generic readtags.php script and have it spit out the data, which is where im at right now. so i could just say:

              http://www.server.com/app/readtag.php?user=phpbuilder and have the php file incorporate the variable into its query and spit out the correct json enocded data, right?

              Yes, by making use of url rewrite (which I've never learnt), that should be possible.

              marciokoko;10964093 wrote:

              One more thing, this is the code im currently using:
              {"id":"4","name":"hers","pass":"","udid":"F6869 FB4-2EBE-5D43-A62D-5D4007646764","timestamp":"2010-09-18 11:15:16","reglocation":""}

              which is a dictionary, correct?

              It depends on how strict you want to be with the term dictionary. Technically dictionary is another word for associative array, and this is a string representation of such an array, rather than such an array. But in the end it should end up as an associative array in javascript.

                10 days later

                johnafm,

                ive reached the following impasse. Im now trying to HTTP POST from the iOS to php file. The php file works because i created an html form that posts to it and it works perfectly, but this code doesnt work from iOS:

                NSString post = [NSString stringWithFormat:@"userudid=&#37;@", [udid stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
                NSLog(@"%@",post);
                NSData
                postData = [NSData dataWithBytes:[post UTF8String] length:[post length]];
                //[udid dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
                NSLog(@"%@",postData);
                //NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];

                NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
                NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://www.myserver.com/myapp/readtags2.php"]];
                [request setURL:url];
                [request setHTTPMethod:@"POST"];
                //[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
                [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
                [request setHTTPBody:postData];
                NSURLResponse *response;
                NSError *error;
                NSData *urlData = [NSURLConnection sendSynchronousRequest:request
                								returningResponse:&response
                											error:&error];
                
                NSString *content = [NSString stringWithUTF8String:[urlData bytes]];
                NSLog(@"responseData: %@", content);

                  Your chances of getting help with this would increase if you posted on an iOS developper forum instead.

                    2 months later

                    johnafm,

                    If you are looking to store user data on a MySQL database, you might want to have a look at a Beta system we are building called Kumulos - www.kumulos.com.

                    Basically it allows you to set up database tables to store data, then automatically generate API methods to access them. The problem you are facing would be pretty trivial to solve with Kumulos implementation is straight forward both server and client side as the data you receive back from the system is already in an NSDictionary.

                    Oh, and it also generates all the Objective-C code for you 🙂

                      Write a Reply...