In the blog post I referred to, the MySQL query comes back to PHP as an Associative Array. This is very similar in nature to a Lua table, i.e. key-value pairs. However you have to transform the data into something that can be passed in an HTTP request, which generally requires that data being converted into a web-safe string. The best way to do this is to use JSON (JavaScript Object Notation). Both Corona SDK and PHP support API calls to encode tables/arrays into a JSON string and then decode them back into their native table structure.
The basic flow is this:
Corona SDK makes a network.request() to the PHP script on the server.
The PHP script makes the database query, gets back the associative array.
The PHP script calls json_encode() on the array which returns a string: $myJSONstring = json_encode( $myReturnedDataBaseArray );
The PHP script uses it’s “echo” statement to print the string out to the Web Server’s output stream.
Corona SDK’s network.request() recogonizes it got data on that stream, reads it in and calls your listener function that you defined in the network.request() call. The data returned by the script will be in the “event.response” variable.
In the listener script convert the event.response, which should be the encoded JSON string and turn it back into a Lua table:
local json = require( “json” )
local myDatabaseTable
…
local function handleDatabaseQuery( event )
if event.phase == “ended” then
myDatabaseTable = json.decode( event.response )
end
end
network.request(“http://path.to/yourscript.php”, “GET”, handleDatabaseQuery)
or something similar to that. Of course there is more to the handleDatabaseQuery function. You need to check for errors and so on, but that’s the basic flow. At the point the handleDatabaseQuery function is called, your table (that I named myDatabaseTable) will have the results of the query from the PHP script. If your query was something like: SELECT name, address, age FROM person where ID = 12, then the result should be a lua table with members, name, address and age:
print( myDatabaseTable.name, myDatabaseTable.address, myDatabaseTable.age)
or something like that.
Rob