Create an API (PHP)

TinyWebDB is an App Inventor component that allows you to access the web from an Android app. You can use TinyWebDB to access a data source (API) or to store the app’s data persistently in a web database. These notes show you how to do the former– create an App-Inventor-compliant API that returns data to an App Inventor app. Often, the service you write will just serve as a proxy and call some other existing data service (e.g., Twitter, Amazon, Yahoo Finance).  

Though you can create an App-Inventor-compliant API in many languages and environments, these instructions describe an API written in PHP.  

To follow these instructions, you’ll need to have some programming expertise and be familiar with PHP and web services (APIs).  

 
App Inventor TinyWebDB Protocol  

TinyWebDB provides two key functions: StoreValue(tag,value) and GetValue(tag) which allow an app to store and retrieve tag-value pairs.

In this tutorial we shall create a small app that allows us to store a result, and recall what we stored. 

Although you can do that with standard TinyWebDB / App Engine usage in App Inventor,  so we’ll do a little extra.

If the value we store is the string ‘two times table’ then let’s actually return the two times table. 1×2=2, 2×2=4 etc etc when we recall it. In other words, showing we’re processing data ahead of returning it, not just returning static items parrot fashion.  

App Inventor Part

OK, so start a new project and call it ‘PHPTest’, and set the title of Screen1 to ‘PHP TinyWebDB demo’.  Add the following components:

 Textbox1 

  • Empty the Text, so it’s blank
  • Set Hint to “Enter the text to be sent and stored. Send ‘two table table’ without the quotes to get the two times table”
  • Set Width to Fill Parent

 Button1 

  • Set Text to Submit

 Button2 

  • Set Text to Retrieve

 Label1 

  • Empty the Text, so it’s blank.

  TinyWebDB1 

OK, now open the Blocks Editor and let’s make the magic happen. 

Define two global variables: 

  

Wire up the two buttons. Start with Button1, which was ‘Submit’. Set it up as below:

   

Basically it’s going to make a call to StoreValue in the TinyWebDB component, passing in the tag ‘testPHP’ and the text contents of the textbox.

 Now Button2, which was ‘Retrieve’. Set it up as below: 

  

Passing in the same tag as we used to store the value, so we can use it (if we want) to identify which StoreValue is related to which GetValue.

Now let’s set up the TinyWebDB component.

First, let’s be good and catch errors that may be reported, that we can push to the users screen in Label1.  

Now the guts of it. Set is up as follows:

   

Basically, this simply builds up a string called theText that is ultimately set to be displayed via Label1.  

The guts of this is a while loop while there is still data to be read (items in the list), get each one out, and add it to theText. If you’ve set the TinyWebDB component to point to our PHP servers as mentioned above then you should be able to store and retrieve values to your hearts content without doing anything else. Don’t forget, store ‘two times table’ (case sensitive, without the quotes) and retrieve it to see the two times table and not just a straight text retrieval. 

TRY IT. Scan the barcode to your Android phone:
CUSTOMIZE IT. Download the source code blocks to create your own PHPTest app.

  1. save the source file to your computer
  2. open the My Projects page in App Inventor
  3. select Upload Source
  4. choose this file
   
 

PHP Part

Well, the PHP for this is remarkably simple. 

   
CUSTOMIZE THE API. Are you a programmer? If so, download the source code for the PHP  Part

What does it actualy do? First, get the Post string: 

 $postUrl=$_SERVER["REQUEST_URI"];

 and see if it is a store request:

 if(strpos($postUrl,'storeavalue')){

then get the variables that were passed in: 

  // Get that tag
  $tag = trim($tag);
  // Get the value
  $value = trim($value);

then do your processing, or whatever. If it’s a get request this will result in you creating an array of all the data you want to return: 

 $resultData = array("VALUE",$tag,array($theData));

where $tag is supplied by default, and $theData is the data you wish to return. Then prime a JSON formatted string using json_encode(), and return it:

$resultDataJSON = json_encode($resultData);
echo $resultDataJSON;

A simple string would return something like: 

["VALUE","tagname",["stored string"]]

Or a more complex array (like our two times table) could result something like: 

["VALUE","tagname",{"1 x 2 = ":2,"2 x 2 = ":4,"3 x 2 = ":6,
"4 x 2 = ":8,"5 x 2 =":10}]

So as you can see, strings in quotes. Numbers not in quotes. Be careful of Gotchas!! A couple of things that stumped me during this:

  1. Cutting/pasting text into PHP caused odd line breaks which upset PHP. Make sure all lines are valid.
  2. json_encode is PHP5 only. If you can’t use that, construct the string manually, so long as it is a valid JSON string as shown in the examples above.

Also, don’t forget to point your TinyWebDB component to your correct PHP file.

I strongly recommend while you start playing to dump out to a MySQL database or a flat file, the contents of $_SERVER["REQUEST_URI"], $tag and $value so you can see what is being passed in etc. It makes debugging a lot easier.

The world is open to pull data into App Inventor. All you need is to format it as a valid JSON string as shown in the examples above.

Have fun!!

This sample was inspired by the post ‘TinyWebDB php example’ on the official App Inventor for Android Google Groups, and thanks to the author of that post (martin12345) for his help and input.

Next steps:

I’m working on a tutorial that will do this all slightly different, and not use TinyWebDB.StoreValue any more, and rather have an App Inventor Procedure that compiles a JSON string that is sent in as the tag on a TinyWebDB.GetValue, deconstructed in PHP, does some jiggery pokery and returns another JSON string. I’ll have this out very very soon.   

Martin Keywood

58 Responses

  1. Looking forward to this! Adding this comment so I will be notified when this is updated. Thanks!

  2. This is awesome! Thanks for taking the time to put this out here.

    Forgive me for being new at this, but just to make sure I understand what’s going on here I have a question.

    This app sets the TinyWebDB ServiceURL to point to the PHP script rather than an actual TinyWebDB database. As such, anything $resultData is set to in the PHP is returned as valueFromWebDB in the app. Is this correct?

    If this is correct you have just solved a HUGE problem for me that I have been looking for a solution to since I first got on appInventor.

    • Yep, you are absolutely right :)

      Well, to be more accurate, what is returned in valueFromWebDB is actually either some text, or a list, depending on what is sent in the $resultData JSON string.

      For example, if it returns: ["VALUE","tagname","element1"] then valueFromWebDB will be text.
      However, if you return [“VALUE”,”tagname”,["element1","element2"]] then valueFromWebDB will be a list with “element1″ in index 1 and “element2″ in index 2.

      Therefore, in my example, I always return the bit after $tag as an array by doing $resultData = array(“VALUE”,$tag,array($theData)) even if it’s a single piece of data, because that way there is no ambiguity at App Inventor end as to whether it’s a bit of text, or a list.
      It’ll always be a list.

      Hope that makes sense.

      Have fun. A new Sample App will be going up very soon that will have a bigger framework for App Inventor and PHP integration.

      Hope you like it :)

  3. Hi
    Thanks for a good sampel app, but I have a problem, if I use your webserver it’s working OK, but whe i try my own, server running Microsoft server, and using asp, I got a errore:
    Communication whit the web service encountered a protocol execption.
    Can you expland what it means!
    Thenks

    /hyde

    • Hi,

      I’m not using a Microsoft Server, but it should still be the same.
      Basically, the problem will be that your PHP is failing, for one reason or another.
      It’s more than likely invalid PHP causing a Server Error (500), and nothing to do with App Inventor at all.

      Tracking this down can be fun!!

      Here are a few pointers that I hope help. All of this is in a browser, and not in App Inventor:
      1. If you have cut/paste the PHP lines in, make sure the line wrapping (or apparent line wrapping) is valid, and that every line is valid PHP code
      2. Make user you are using PHP5 if using json_encode, otherwise you will have to construct the equivalent sting manually
      3. Try going to the page directly in the browser (the one you are pointing to in ServiceURL in TinyWebDB) and see what it returns. If you do my one there it will be something like [“VALUE”,””,["bbbb"]].
      4. If it doesn’t return a JSON string like that (maybe even shows a 500 Server Error), then you need to try and identify what is going wrong
      5. I tend to comment out EVERYTHING and run it again in the browser to see if the 500 Server error goes away. It will, if it’s valid PHP
      6. Then I reintroduce a bit of code at a time and echo out variables etc and keep refreshing the browser page and keep repeating this until I find out what is breaking it, then fix it.
      7. Ultimately, you will have a PHP page that if you go to in the browser will return a JSON string like that above. When it’s returning that, your App Inventor error will go away and you just need to make sure you’re handling the data correct in App Inventor, as per the tutorial.

      Hope that helps, and good luck

      Martin.

  4. Hi Martin

    Thanks for your reply, but I use an asp page, and as you write it should be the same.
    I have try to call an empty page whit the same result.??
    Are tinywebdb using port 80?

    Thanks
    /hyde

    • Are you trying to call an empty page in the browser or through App Inventor?
      I think an empty page back to App Inventor would cause the same thing as its invalid return data.
      The key is to make sure the returned data is a valid JSON string like those above.

      Is your ASP page when called in a browser returning the same looking data as if you call my page in a browser, as per the examples?

      As for port numbers, I don’t know, sorry, but I know I didn’t have to do anything special.
      Maybe its to do with the headers / MIME type but I’m afraid I don’t know as that didn’t seem relevant to the PHP version.

      If I get a chance I’ll try and set up an ASP rig and take a look.

      Good luck

      Martin

  5. Hi Martin
    I have now tried the Tint Web Weather app, it’s the same error?
    I have mad a script returning the same JSON string as the Weather server.

    You can try it here

    http://www.fiskerforum.dk/testdb.asp

    /hyde

    • Hi,
      I’m not sure why that would be, I’m afraid. My ASP is not that strong. I’ve done a search of the Google group for App Inventor looking for the appropriate MIME types, to see if that’s more pertinent to ASP to use and it shows both
      Content-type: application/jsonrequest and
      Content-type: application/json as possibilities in the post “Tinywebdb custom server on php”. Maybe they’d help?
      Try running my page in a browser, copy the output and have an ASP page that just returns that and nothing else. See if that works.
      Other than that I’m not sure. Let’s hope an ASP expert comes back with something on the Google User Group. Please keep me posted. I’m intrigued :)
      Cheers
      Martin

  6. Martin,

    I just can’t seem to get what I am trying to do to work and I am losing sleep over this :(

    I have tried starting with both test and test2, and stripping them down to the basics, yet I can’t get what I am trying to do to work via PHP.

    Here’s a simple function I’d like to have working. With this working I could better understand what is going on. I know it’s basic, but I can’t figure out where I am going wrong:

    On the app screen there is an input box and a SUBMIT button.

    A number is entered into the input box, and when submit is pressed the result of the number entered multiplied by 9 is displayed back. But, I want the calculation done in a PHP script, not in the app.

    So simple, yet I can’t get it to work.

    Any help would be greatly appreciated.

    Jay

    • Hi,
      It can be frustrating. Hang in there.
      So, a couple of questions. I presume test1 and test2 both run if they point to my servers.
      Have you downloaded the PHP of those and hosted them on your server, and they run?
      If not, you have some fundamental issues on your server, ie MIME types allowed do not include JSON (although shouldn’t be a problem as technically just sending/returning a string), or your server is not PHP5 so json_encode is failing, etc.
      If my php code does run on your server, then we’re OK and we can get yours working.

      Test1 and Test2 use different approachs.
      Test1 uses StoreValue and GetValue and returns an array.
      Test2 uses GetValue only but passing in a full JSON string (tag AND data) via the tag part of GetValue, which is decoded in PHP and also returns an array.

      I have build an App that you are trying to do, but used the simplist approach. This is to call GetValue but the tag we pass in is the number we want multiplying by 9.

      App Inventor App is simply a Button, a Textbox, a Label and a TinyWebDB.
      Set the Service URL of TinyWebDB1 to the file called simpletest.php in the same location as the other samples (mwk.freehostia.com/appinventor).

      Blocks Editor is just two things.
      Button1.Click calls TinyWebDB1.GetValue with tag Textbox1.Text
      TinyWebDB1.GotValue sets Label1.Text to valueFromWebDB

      That’s it. NOTE though that this is assuming the JSON string returned is a string and not an array. Ie ["VALUE","3",27] not [“VALUE”,”3″,[27]] . See the difference with the data part being an array in the second one?

      The PHP itself is also very small: (obviously put the PHP tags around it I can’t put that in this reply comment as it upsets WordPress :( )

      $tag = trim($tag);
      $resultData = array(“VALUE”,$tag, ($tag * 9) );
      $resultDataJSON = json_encode($resultData);
      echo $resultDataJSON;

      Basically get the $tag, which we know in this example is our number.
      Then build our data which is the string “VALUE”, $tag and ($tag * 9), ie our logic.
      Then JSON encode it and echo it out.

      If json_encode is your problem (ie no PHP5) you can build the JSON yourself so replace the lines:

      $resultData = array(“VALUE”,$tag, ($tag * 9) );
      $resultDataJSON = json_encode($resultData);

      with

      $resultDataJSON = “[\"VALUE\",\"$tag\"," . ($tag * 9) . "]“;

      ie building it manually.

      If you can put the URL of your PHP page in a browser and it returns a JSON string like ["VALUE","3",27] then you’re going to be fine. If it doesn’t, then the PHP is wrong.

      Also, on a side note, I have had times where the emulator does not work, but if I use the phone it will work fine. It seems random and unpredictable, so I use the phone every time now.

      Good luck

      Martin.

      • I appreciate your time and attention to this so much, Thank you.

        So, here’s what I have now:

        When I point my app to mwk.freehostia.com/appinventor/simpletest.php it works great, so the problem is not my app.

        When I put mwk.freehostia.com/appinventor/simpletest.php in my browser I get:
        ["VALUE","",0]
        which is as it should be.

        When I copy the PHP above exactly and add PHP tags at start and end the app returns 0, and when I put the link to the PHP directly into my browser the app returns a 0, and putting the PHP link directly into my browser results in:
        [null,"",0]
        which is not correct.

        So, I replaced the two json lines with $resultDataJSON = “[\"VALUE\",\"$tag\"," . ($tag * 9) . "]“;

        and when I put the PHP link in my browser I get:
        Parse error: syntax error, unexpected ‘[‘ in /home/hpdj/gic4u.com/ez1.php on line 3

        I did everything I could think of to correct the syntax of the line, but my PHP is not the greatest :(

        I am so close now, but something is still not right.

        Jay

  7. I just reread my post above and I see it had a paragraph that may have been confusing. To clarify:

    When I copy the PHP above exactly and add PHP tags at start and end the app returns 0 when I run the app for all values entered in the input box, and when I put the link to the PHP directly into my browser it results in:
    [null,"",0]
    which is not correct.

  8. Just tried installing your application from the barcode. When I enter two table table I get
    communication with the web service encountered a protocol exception.

    any thoughts?

    • Hi,

      I just tried it and it seems to work.
      Do you get the error when you try and submit the string, or retrieve it?
      Do you get the error if you run it through App Inventor, ie download the source and upload in App Inventor?
      It could be the server was down or unavailable at the time you tried it? I have had that problem before.
      Maybe try again or try it through App Inventor and see if it works.

      Cheers

      Martin

  9. Martin,

    Wonderful example – thanks for taking the time to write this up. The only thing not clear to me (and not working when I put the script on my server) is where $tag and $value come from. When I change the write to this:

    fwrite($fh, “posted: ” . $postUrl . “, tag: ” . $tag . “, value: ” . $value);

    I get the following ($postUrl is fine, but $tag and $value are empty):

    posted: /android/test_comm.php/storeavalue, tag: , value:

    I don’t get where the script is supposed to be picking them up from.

    • Hi,

      Thanks :)

      Tag and Value are basically the text you are passing in in TinyWebDB1.StoreValue or GetValue in the tag or valueToStore parameters.

      Have you tried getting them direct from the POST array:
      $tag =trim($_POST["tag"]);
      $value =trim($_POST["value"]);

      I like to dump them out to a flatfile with something like:
      $postUrl=$_SERVER["REQUEST_URI"];
      $tag =trim($_POST["tag"]);
      $value =trim($_POST["value"]);
      $myFile = “testFile.txt”;
      $fh = fopen($myFile, ‘w’) or die(“can’t open file”);
      fwrite($fh, str_replace(‘”‘, ”, $postUrl));
      fwrite($fh, str_replace(‘”‘, ”, $tag));
      fwrite($fh, str_replace(‘”‘, ”, $value));
      fclose($fh);

      Someone recently had a similar problem where a new PHP file created in Notepad appeared to run properly but was not allowing access to the POST variables (all just null), but using an alternate editor to create the file worked fine. It was very strange and took a while to find, but completely true, so I thought I’d mention it in case it was something equally as bizarre here.

      Also, I have had wierd problems using the emulator too, so might be worth using an actual phone if you’re not already.

      Hope this works for you.

      All the best

      Martin.

  10. forgive me for asking something that maybe obvious – I am getting the protocol exception error running to an iis server. I am getting the correct data in the URI from the phone (wireshark) but it seems that the php page/server is not handling the data correctly. what exactly is needed to run AI to a php page on windows server – i am all out of guesses
    thanks
    nelson

    • Hi. Sorry I have not used a Windows server so I’m afraid I don’t know. Sorry about that.
      Best of luck
      Martin

      • Thanks for the reply – after a week of struggling with window security,JSON compatability and many other little things, I moved it over to a linux server and in 5 minutes it was working.
        thanks again for sharing with us…
        nelson

  11. Just playing around and everything works good when I use your ServiceURL in the TinyWebDB. When I change it to mine, I used your write debugging example and there are no values coming over in the POST. When I manually insert ‘two times table’ into my file, everything works great. You had mentioned in one of the comments above this happened to someone else…what was the resolution? Thanks in advance

    • Hi. The general consensus was that it was a setting on the PHP install on the server. But not able to determine exactly what. Sorry I cant be more help.

  12. Hi,
    Is the code under the GNU GPL v3 (or another open source license)?
    I would really like to use it in my application.
    Thanks,

  13. I need your help are advise. I want to get small bit of info off a web site that has PHP for the answers to my APPs question I have all the ground work done for design and layout but I have read all of these site and it is starting to all run together in my head. LOL I have set up my app engine with python and deployed myapp site to google. I have set the tinywebdb in my app on AI. here’s the confusing part. I can activity starter and just pull up the site with the info, but there is more there than I want. when I view sourse it falls only on line 119 of there script. Its the same everytime they update the site witch is every monday.

  14. I’m getting only false back, when i upload my php file to my own server.
    And the tiny url is set correct..
    It works well with your url.

    • Hi,
      There have been a few responses where people have had trouble getting PHP working and it seems to be down to the version of the PHP install and the settings on the PHP install on the server. But not able to determine exactly what. Sorry I cant be more help.
      One thing I recomend though, is in the other topic on this site, ‘Create an API (PHP) Application Framework’, I responded in the comments with a list if dubugging things I have recomended in the past. These may help.
      Good luck and all the best.

  15. I get the an error when i try running this from my site

    [“VALUE”,””,[false]]

    I have verified with BlueHost that i have json lib enabled but its not writing to the testFile.txt

  16. please let me know if there is a different fopen command for php 5 or a fix to your test1.php

    • Hi there,
      There is nothing special about the fopen, and the test1.php source on here is unaltered.
      I use Freehostia to host my pages, and when I first use fopen it creates the file with permissions that allow me to write to it, so in other words I don’t do anything clever.
      Freehostia does allow me to edit file permissions though, but as I say it seems OK out of the box.
      I’m not sure what to suggest, I’m afraid.
      One thing I recomend though, is in the other topic on this site, ‘Create an API (PHP) Application Framework’, I responded in the comments with a list if dubugging things I have recomended in the past. These may help.
      Good luck and all the best.

  17. Does anyone know, where I can find a PHP script, which do a storevalue and a getvalue on my on web page / mysql in the same manner as if I was using googles service.

    Kind regards
    JmExperia

  18. The value i’m getting from the $_POST(“value”) is all escaped out like below:

    “02\/01\/11\r\rCOVERED (7) \”SUSHI AVENUE\” ON WAY FINDING SIGNS AND REMOVED (1) DOUBLE FACE SIGN IN FRONT OF THE RESTAURANT. TEST AGAIN.”

    When I save it to the field in my database is keeps all the escape codes so the formatting is all messed up. Is there a way to decode this string so when it saves to my database string field it formates correctly?

    • Hi,
      I’m not sure, to be honest, but I wonder if the php function urldecode() could be used?
      Or some similar way to tidy the data. Perhaps using regular expressions?
      All the best
      Martin

  19. I am trying to “getvalue” and I am returning a string 100% identical to your example (A simple string would return something like:
    [“VALUE”,”tagname”,["stored string"]]), but I just get an error saying “Runtime Error. argument #1 (stored string) to ‘cdr’ has wrong type (java.lang.String)(java.lang.String)
    Can anyone explain, what that means?

    Kind regards
    Jakob

  20. First, thanks so much for your wonderful tutorials. You are really helping to make a fairly complex system much easier for us non-techies to understand. THAT is no small achievement!

    Second, I hate to treat you like tech support, but after saving your php code directly to my home server (WAMP 2, with all updated settings incl. php 5.x & trying both localhost, and over the internet approaches) AND trying it on my Godaddy web hosting account, (with wide open settings), I absolutely CANNOT get this code to work.

    When I manually enter the address in a browser, I get: [“VALUE”,””,[false]] as the response, and through my EVO 4g attached to app inventor I get is: “communication with the web server encountered a protocol exception”

    However, through your server everything is peachy.

    I have tried your troubleshooting techniques to no avail. Does ANYONE have any ideas? I want to play with this and see how/why the different parts work, but unless I have access to working php code on a server I can modify, I will not be able to.

  21. Wow dude do you realize how broken the php file “test1″ is? I just spent forever trying to figure out what the problem is and as im trying to learn this and am clueless I was hoping your source would be informational… I guess in the long run I did learn something but a simple read over would have saved time… your php file lacks the entire part where you get the POST… the most important part… $tag =trim($_POST["tag"]);
    $value =trim($_POST["value"]);

    • Thankyou for this.
      This has been used by many many people successfully, but you do raise a good point.
      Depending on PHP configurations, which seem to vary immensely, this may indeed be needed.
      Thanks again.

  22. Aha, it seems my instance of test1.php needed these lines to get any values into testFile.txt

    $tag =trim($_POST["tag"]);
    $value =trim($_POST["value"]);

    And I must say I understand this code better that then magically appearing $tag … ;-)

    Remark: it’s probably safer to have only one line saying

    $myFile = “testFile.txt”;

    … so I created at the beginning of the file, and removed the other two.

  23. Dear Author – Please help. Above, in your PHP, you have “storeavalue” but in the API fo the actual TinyDBComponet here… http://appinventor.googlelabs.com/learn/reference/components/notready.html#TinyWebDB …it is actually called “StoreValue” and I am wondering why you have something different than “storevalue” when you grab from the URI? Is that a typo in your PHP or is there something that I am missing? Please advise. Thank you. – Mark Kamoski

    • Hi,
      Well spotted :)
      When I first started playing with it I did so by calling php pages from app inventor and writing out the content of $_SERVER["REQUEST_URI"] to a file.
      This showed it as storeavalue and that seems to work well.
      I’m sure there is someone out there who can give a more technical reason :)
      Cheers
      Martin

  24. Martin,
    I have been going over your php, and I can’t seem to get this to work on my server. I connect, but when i enter data into the text box, I get \two times table\ as an output.

    Any ideas? I can send you the code if you need it.

    Thanks,
    Jason

    • Hi Jason,
      I have seen something like this before and it was something to do with string escaping.
      I’ll try and remember.
      Do you get the same results if go through app inventor and just a web browser on your pc?
      Martin

      • Yes, but i finally figured it out. In the str_replace, i had to add an array, not just a single value to get rid of both the ” and the \\
        fwrite($fh, str_replace(array(‘”‘,’\\’),”, $value))

        this worked…

        On a different note, I am very interested in learning how to get stored value from a mysql database. I have been messing around with php and was hoping to figure it out…but to no avail.

        do you have or know of a good tutorial for this?

        Thanks,
        Jason

  25. Hi,
    Using mysql etc is really quite simple. There are tons of good mysql basic tutorials out there and then when you retrieve the data you just format it is a JSON array like you would the returning data in the other examples.

    For example, using a fictional Customers table in a fictional database called “your_database” with username “db_username” and password “db_password” we will do a fuzzy search for people with a surname that matches the value passed in from App Inventor (set as the tag):

    <?php
    mysql_connect("mysql2.freehostia.com","db_username","db_password");
    @mysql_select_db("your_database") or die( "Unable to select database");
    $searchFor = str_replace('"', '', $tag);
    $query="SELECT Surname, Forename FROM Customers WHERE Surname like '%$searchFor%'";
    $result=mysql_query($query);
    $num0=mysql_numrows($result);
    $i=0;
    while ($i < $num0) {
    $theData[$i] = mysql_result($result,$i,”Surname”) . ” ” . mysql_result($result,$i,”Forename”);
    $i++;
    }
    $resultData = array(“VALUE”,$tag,$theData);
    $resultDataJSON = json_encode($resultData);
    echo $resultDataJSON;
    ?>

    So it does the search, loops through all returning records and builds up an array, JSON encodes it and returns it.

    Hope that helps

    Martin.

  26. Hey

    Can any1 tell me, why this doesnt work?

    include ‘dbconn.php’

    $postUrl=$_SERVER["REQUEST_URI"];

    if(strpos($postUrl,’storeavalue’))
    {
    $tag =trim($tag);
    $value =trim($value);

    $value1 = str_replace(‘”‘, ”, $tag);
    $value2 = str_replace(‘”‘, ”, $value);

    $sqlstring = ‘insert into test (tag, value) values(“‘value1′”,”‘$value2′”)';
    $result = mysqli_query($link,$sqlstring);
    }

  27. Hi jason,

    Thank you for the very good tutorials but i have one request.
    Do you know how I can add more textboxes so when I press submit the test1.php will give the value for each textbox.
    For example we have 2 textboxes in the app name, surname (or more) and when I press submit and refresh the php page i will get:
    name: John
    surname:Smith
    …..

    I’m trying to use your app to write through php to mysql db .

    Thanks,
    Panos

    • Hi,
      If you have a look at the other php sample (application framework) it has an example of submitting multiple elements into php.
      Hope that helps
      Martin

  28. Hello there,

    very awesome work there!
    This actually could be the key for an project I’m trying to do:
    I need to get Values from an Mysql DB and set Values back.
    In “earlier” times I used CSV formated PHP files which we’re hard to parse. This seems to be an more economic solution.

    I got your files and set them up and I can write and read to your server. But I can’t write to mine. I can only change the TestFile.txt content, upload it, and read it. Submitting stuff ain’t working. Are you sure about the test1.php (everything alright with that?) or am I doing something terribly wrong?

    On submit, the system seems to overwrite the TestFile.txt – but with empty stuff, so I’m not sure wheter that thing does really get the tag and value in php.

    ( if(strpos($postUrl,’storeavalue’)){
    // Storing a Value

    // Get that tag
    $tag = trim($tag); <- where to get that from? $tag does not exist!
    // Get the value
    $value = trim($value); <- where to get that from? $value does not exist!

    // In this example, output to a file, but could use MySQL, or anything
    $myFile = "testFile.txt";
    $fh = fopen($myFile, 'w') or die("can't open file");
    fwrite($fh, str_replace('"', '', $value));
    fclose($fh); )

    Greetings,

    Nico

  29. I did grab the $_SERVER["REQUEST_URI"] dirty and easy:

    // In this example, output to a file, but could use MySQL, or anything
    $myFile2 = “testFile2.txt”;
    $fh2 = fopen($myFile2, ‘w’) or die(“can’t open file”);
    fwrite($fh2, $_SERVER["REQUEST_URI"]);
    fclose($fh2);

    Result was:

    /test1.php/storeavalue

    But i don’t see any tag or value, so maybe the app doesn’t send that correctly?

  30. Ok I figured it out, the test1.php file has an error!

    // Storing a Value
    // Get that tag
    $tag = trim($tag);
    // Get the value
    $value = trim($value);

    is wrong, it should read:

    $tag = trim($_POST["tag"]);
    $value = trim($_POST["value"]);

    and the same for

    // Retrieving a Value
    //$tag = trim($tag);

    should be:

    $tag = trim($_POST["tag"]);

    And then – it worked, magically ;-).

    Thanks for your great project!

  31. Hi,
    Glad you got it sorted.
    Strangely on my PHP Server I don’t need to do that at all and test1.php works fine, but others have needed to do what you have done there.
    Glad it worked for you
    All the best
    Martin.

    • I’m completely new to app inventor and even newer to PHP, but this seems to be exactly what I need. I want to create a app that will basically run or communicate with already existing PHP code. Now, from what I can tell (I haven’t actually done this whole tutorial yet), is the label what displays what the app retrieved from the PHP code?

      Also, if I wanted a list of lets say little paragraphs, could the App inventor make it so it can be viewed together on the entire screen of the phone and just have it scroll down to view more? It seems that you would have to view it one at a time with the one label right?

      Thanks

  32. hi guys.
    may i ask how to store a image into file and store its path to the phpmyadmin database?
    whats the coding and the block?
    i have no idea in it.
    i tried the method which use to store text but its not working.
    pls help me.
    thanks…

  33. hi…
    can any1 tell me how to store a image into phpmyadmin by using php code and tinywebdb?
    can show me how to write the coding for it and set the block?
    i tried the way of store text but its not working.
    i need help.
    thanks every1.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 59 other followers