Hello everyone,
So I finally managed to fix some stuff regarding Databases and writing to .txt files in AS3. There’s one small problem left, though.
I am now trying to write data from AS3 to a PHP, that transfers it into a mySQL database. I have 2 working .php files, one to write to the Database (writeDB) and one to read out of the Database (readDB).
I can’t seem to call the writeDB.php file.
Here’s my AS3 Code regarding only the .php related stuff:
//INITIALIZATION OF VARIABLES ETC.\
As the header indicates, this initializes the variables I need.
I have no idea if something is missing. If there is, notify me :x
var variables:URLVariables = new URLVariables();
var writeLink:URLRequest=new URLRequest("PATH TO PHP FILE");
var phpLoader:URLLoader = new URLLoader();
public function endScreens():void{
connectionLink.method = URLRequestMethod.POST;
connectionLink.data = variables;
phpLoader.dataFormat=URLLoaderDataFormat.VARIABLES;
phpLoader.addEventListener(Event.COMPLETE, phpLoaded);
variables.Name = "";
variables.Score = 0;
}
//MOUSECLICK\
This little bit gives the Name variable (String) a value, and then calls phpLoaded (see below), before removing the input-screen to go to
the scoreBoard.
if(endArray[6].buttonMode){
Name = endArray[6].text;
phpLoaded(e);
for(var k:int = 3; k < endArray.length; k++){
removeChild(endArray[k]);
endArray[2].visible = true;
endArray[6].buttonMode = false;
}
scoreBoard = true;
}
//ONCE MOUSECLICKED\
This piece of code is SUPPOSED to send the player’s Name and the Score attained to the writeDB file.
public function phpLoaded(e:Event):void{
variables.Name = Name;
variables.Score = Score;
phpLoader.load(connectionLink);
}
Now for the PHP code:
//WRITE TO DATABASE\
<?php
$host="HOST;
$user="NAME";
$code="PASS";
$database="DATABASE_NAME";
$table="TABLE_NAME";
$connection = mysql_connect($host, $user, $code) or die ("404: Can't connect to ".mysql_error(mysql_select_db($database)));
mysql_select_db($database);
$playerName = $_POST['Name'];
$playerScore = $_POST['Score'];
$assignment="INSERT INTO $table (`Score`, `Naam`) VALUES ('$playerScore', '$playerName')";
$execute = mysql_query($assignment);
echo $assignment;
?>
//READ FROM DATABASE\
<?php
$host="HOST;
$user="NAME";
$code="PASS";
$database="DATABASE_NAME";
$table="TABLE_NAME";
$connection=mysql_connect($host, $user, $code) or die("404: Cannot Connect");
mysql_select_db($database);
$assignment="SELECT * FROM $table";
$result = mysql_query($assignment);
$rows = mysql_num_rows($result);
$scoreVars = array();
$i = 0;
while ($row = mysql_fetch_array($result)){
$i++;
$scoreVars['Naam'.$i] = $row[Naam];
$scoreVars['Score'.$i] = $row[Score];
echo "<b>Naam: </b>".$scoreVars['Naam'.$i]." ";
echo "<b>Score: </b>".$scoreVars['Score'.$i];
echo "<br></br>";
}
if($i == 0){
echo "No data was found!";
}
$return = http_build_query($scoreVars);
mysql_free_result($result);
mysql_close($connection);
?>
As I said before, the PHP files work.
If I give writeDB’s $playerName and $playerScore standard values, and I run it, those values are added to the database.
If the database has filled entries and I run readDB, the information is echo’d onto my screen.
I also seem to be getting the #2101 error. I looked it up, and the most common cause is a & in front of a variable name in a .txt file, if I’m not mistaken? Yet, I don’t even use a .txt file, let alone a &. So what could be causing this?
It happens when I click my submit button and seems to be tied to
phpLoader.load(connectionLink);
Thanks for any help you people can provide!
Arjin, out!