So the other night I set out to implement the
plan I outlined before to figure out how to push data (i.e. each turn's information) from Actionscript variables to the database. Here are the components I wrote/set up:
- Database: set up a test table called TestTurns with 3 fields - p_id (piece's ID number), xpos, and ypos.
- Actionscript: added a storeTurn() method to the 'piece' class, which makes use of the URLVariables, URLRequest, and URLLoader classes to structure and send an HTTP POST request, containing a piece's ID number, x-position, and y-position, to the PHP file. Also wrote a callback function - which gets attached to the Loader's COMPLETE event - that spits the echoed data from the PHP script (provided in name/value query-string format) into a URLVariables object and "logs" some information into a dynamic text box currently sitting in the middle of the race track. (This is a temporary debugging mechanism, obviously.)
- PHP: wrote a small script in a file which grabs the 3 POSTed variables, runs an insert query on the database, and echoes back the information it received and whether the query was successful.
These components seemed straightforward enough, but I did run into some issues along the way.
- First, I was trying to test the POST to the PHP script by running the Actionscript locally. It was able to pull the file, but in the AS callback function I was getting these strange strings as supposed value counterparts of the names that the URLVariables constructor was able to parse out. I rearranged and tested things for some time until the "well duh" moment: I was expecting PHP to run while sitting on my machine. Oh, wait. :) (To test locally, I wrote a simple text file containing a name/value string, directed the HTTP request to that file, and the URLVariables constructor parsed it just fine.) After this, I uploaded the files so I could test it for real (i.e. with the PHP running).
- I knew that for an insert query, the mysql_query function would return a true or false based on whether the query was successful or an error occurred. However, I didn't know the details on the representation of that boolean, and how it would get converted when echoed by the PHP and then parsed by the URLVariables constructor in the Actionscript. From the output I was getting dumped into the "log" it was appearing as a 1 or 0 instead of the string "true" or "false", so in order that the URLVariables variable for 'success' would actually have a value of true or false (that could then be used in a conditional), I just used the ternary conditional operator (?:) to convert the 1/0 value ahead of time in the PHP echo string.
- Also from the output log I realized that since I was calling (in the handler PHP file) require_once for the db_connect file, whatever was echoed in that file was showing up amidst what the handler file was echoing. Naturally this was screwing up the name/value query string I was trying to create so that the URLVariables constructor (in AS) could read it. I was looking through the PHP documentation for the include/require functions to see if there was a way to suppress the output of the included file, but didn't find anything along those lines. As an alternative for echoing messages of success or error in db_connect.php, I created a variable to store a boolean for connection success. Then, when I require db_connect.php in the handler file, I can store that boolean (i.e. $connect_success = require_once "db_connect.php";) to discover and deal with connection errors.
Once I had figured out these problems, though, it actually worked...! I took screenshots both of the turn data in the test table and the output as it showed up in Flash:
No comments:
Post a Comment