Connect to online MySQL and pull data

Is there any sample code on how to connect to my website (running apache, PHP, MySQL) and do a query then grab the results and display them on my Android/iPhone? I have a web app done in PHP that runs fine on the web, i’d like to code a version in Corona that can access the same database as the web version. I’m just looking for sample code that shows me how to connect and add/edit/delete a record. [import]uid: 8512 topic_id: 9432 reply_id: 309432[/import]

I’m actually just getting ready to do this. I don’t have a specific example, but my plans are to do a REST based call and either return JSON or XML.

In theory, your PHP script should take URL based parameters (a GET request) like:

http://mysite.com/game/index.php?username=xxxxxxx&command=get_scores&format=json

Your PHP script would then have those variables in an associative array:

<?php <br>$username = $\_GET["username"]  
$command = $\_GET["command"]  
$return\_format = $\_GET["format"]  
  
// do your SQL query  
  
$results = json\_encode($your\_query\_result)  
  
echo $results  
?\>  

Then in your Lua you would have something like this:

-- lifted from the API example  
local function scoreListener( event )  
 if ( event.isError ) then  
 print( "Network error!")  
 else  
 myData = Json.decode(event.response)  
 end  
 return true  
end  
   
-- fetch your data:  
network.request( "http://mysite.com/game/index.php?username=xxxxxxx&command=get\_scores&format=json", "GET", scoreListener )  

Of course you have to include the HTTP module and find the Json module and load it as well.

I wish this were a working example for you, but theoretical will have to work until I actually put it together.

Rob
[import]uid: 19626 topic_id: 9432 reply_id: 34570[/import]

I should also add, the new UI modules seem to want XML for table loading and such, but you should be able to do the same with JSON data. Its lighter and at the end you end up with a Lua table to do with as you please.

Your existing app may not be outputting XML or JSON in which case you may have to use the string API to parse the data into something useful. [import]uid: 19626 topic_id: 9432 reply_id: 34572[/import]

Hi Rob,

Could you check my post about uploading file with Lua:

http://developer.anscamobile.com/forum/2011/04/30/upload-file-lua-php

I need some one with PHP background to help fix down this code.

Thank you in advanced.

Admin6 [import]uid: 45018 topic_id: 9432 reply_id: 34734[/import]