7 Replies Latest reply on Apr 24, 2010 2:34 PM by jimmyoneshot

    Need Help Getting A Value Into My App Via xml/php http request

    jimmyoneshot

      I know this may not be the place to ask about php but I thought people may have came across a similar problem plus I'm getting desperate Basically the data for my app is stored in a mysql database table and what I need to do is get the auto increment value of this table into my app via a http request. I'm trying to make a php file which gets this value and outputs this value as xml but I am awful at creating php to do such things and just get constant errors. Can anyone take a look at this and help me out to tweak it so it does what I want please? The table in question is called links and the field/row/column in the table I'm after is called linkid. It's the next autoincrement value of that row outputted as xml is what I want. Here's the php so far:-

       

      <?php 
      header("Content-type: text/xml"); 
      $host = ""; 
      $user = ""; 
      $pass = ""; 
      $database = ""; 
      $linkID = mysql_connect($host, $user, $pass) or die("Could not connect to host."); 
      mysql_select_db($database, $linkID) or die("Could not find database."); 
      $tablename   = "links";
      $next_increment  = 0;
      $qShowStatus   = "SHOW TABLE STATUS LIKE '$tablename'";
      $qShowStatusResult  = mysql_query($qShowStatus) or die ( "Query failed: " . mysql_error() . "<br/>" . $qShowStatus );
      $row = mysql_fetch_assoc($qShowStatusResult);
      $next_increment = $row['Auto_increment'];
      echo "next increment number: [$next_increment]"; 
      ?>
      
        • 1. Re: Need Help Getting A Value Into My App Via xml/php http request
          David_F57 Level 5

          hi,

           

          Getting the next increment number is very bad practise, when that number is given it doesn't assume you will use it, for instance it returns 100, in the meantime someone else has appended a new record. guess what the 100 you have is no longer the next increment number, The only way to get your identifier is to append a blank record then return its id value, if for some reason you no longer require that 'empty' record you can always delete it.

           

          David.

          1 person found this helpful
          • 2. Re: Need Help Getting A Value Into My App Via xml/php http request
            jimmyoneshot Level 1

            I know what you mean but for this particular app only 1 person will be using it. This version is for my client to create and ammend links. My users themselves who will have a similar app will only be able to do things such as favourite links and edit the data etc.

             

            Perhaps you're right though, maybe it would be best in a single php function to add a new blank record, get the max linkid i.e. the linkid of this new record and then delete this record all in the same php function so the value is exact, then the value in my app would be this value +1 (the next auto increment). Is that what you meant and would that value be exact in this case?

             

            Cheers for the advice.

            • 3. Re: Need Help Getting A Value Into My App Via xml/php http request
              David_F57 Level 5

              Hi,

               

              The idea is you create the blank record and once you have the id you insert the id into your array then update the record, no insert needed, as you already own that record.

               

              David.

              1 person found this helpful
              • 4. Re: Need Help Getting A Value Into My App Via xml/php http request
                jimmyoneshot Level 1

                Sorry I'm a bit confused. Do you mean to say that I should create multiple blank records which will act as record slots so to speak which the user can insert data into and when they remove an item the record will just be made blank but still be kept in the table and I could have some sor of filter function in my application which makes it ignore the blank records when displaying data. The problem with this is that  I want the user to be able to add an infinite amount of records.

                 

                OR

                 

                Do you mean to say that I should add a blank record at the end of my table which will serve to provide the highest id value for my app? But the problem with this is if a user adds another record it will increment past this blank record and have the new highest id value so the blank record I made would be invalid for this purpose then.

                 

                Sorry for the confusion.

                • 5. Re: Need Help Getting A Value Into My App Via xml/php http request
                  David_F57 Level 5

                  Hi,

                   

                  step 1. the client wants to add a new record. and sends a simple request for a new record

                   

                  step 2. the server adds a new record to the database (its now blank) and returns its ID number

                   

                  step 3.  the client now has a proper ID number and uses it to add a new object to the array

                   

                  step 4. the client armed with a record ID and new data for that 'empty record' does a record update not an insert.

                   

                  step 5. you now have the array and the database in sync.

                   

                  David.

                  • 6. Re: Need Help Getting A Value Into My App Via xml/php http request
                    jimmyoneshot Level 1

                    Gotchya. So what I'd basically be doing is reserving a slot for the link by adding in only the linkid and leaving the rest of the fields for that record blank whereas my auto-increment method would have just been basically finding out what the next slot is but leaving it up for grabs to other users. This wouldn't really matter in this app with there only being one user and one app but you're right it is bad practice and I can apply the method you've outlined to my user app in which case there would be multiple users and multiple instances of the same app i.e. a chance of such data overlaps occurring.

                     

                    Great idea David. Cheers.

                     

                    I'm just trying to figure out a way to get all that to happen in one smooth click of the add button. In my php code I have a line which adds in a new record I suppose I'll have to edit it so that before this happens the record with just the linkid value and the rest of the fields blank is added followed by a bit of code which outputs this new linkid as xml. Then I could retrieve this value in a result handler in my app and in the result handler would be the code which inserts the user inputted data in my app into the empty fields of this new link via http request closely followed by the code which creates the new array collection item containing this new link id value and the same user inputted data so they are both in sync as you say. That sound about right?

                    • 7. Re: Need Help Getting A Value Into My App Via xml/php http request
                      jimmyoneshot Level 1

                      Actually I think I've got an even simpler solution that wouldn't need the blank record:-

                       

                      - When the user clicks the add button it sends the http request to add the new link to the database. The link will auto increment.

                       

                      - Following this I could get my php file to output as xml the linkid of this record which would be caught by a result handler in my app which would save this data as an object proxy.

                       

                      - The result of this result handler could contain the additem code which would add this same data that has just been added to the database  from the object proxy to my array collection.

                       

                      The problem here is how do I output the values of a record that has just been added? I am awful with php :-(

                       

                      Here's how I insert a new record via php:-

                       

                      $query = "INSERT INTO links(categoryid, label, icon, url) VALUES('$param_categoryid', '$param_label', '$param_icon', '$param_url')";

                      Obviously the linkid doesn't need to be entered as it is created automatically by the database table via auto increment but what I'd need it to do directly following this insert is output, as xml, the linkid, categoryid, label, icon and url of THAT item that has just been added. But how exactly do you get these values of an an item that has JUST been added via php code?