Using PHP cant pass a variable from MySql to Flash

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
djburner
Forum Newbie
Posts: 2
Joined: Sun Dec 11, 2011 10:24 pm

Using PHP cant pass a variable from MySql to Flash

Post by djburner »

Hi there, I m totally stuck on this issue a week ago.
I have a php file that handles 3 variables send from Flash, if User and Password are ok, then a score (third variable) is goign to be store in my db, but I cant get nothing in the output of my aS3.

Code: Select all


<?php
$host="mysql.myweb.com"; // Host name
$username="xxxx"; // Mysql username
$password="xxxx"; // Mysql password
$db_name=xxxx"; // Database name
$tbl_name="jos_users"; // Table name

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

// username and password sent from form
$myusername=$_POST['username'];
$mypassword=$_POST['userPassword'];
$score = $_POST['score'];

if(empty($_POST['username']))
{
$error = 'No username';


}elseif(empty($_POST['userPassword'])){
   $error = 'No Password';
}

// To protect injection
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);

if (isset($_POST["username"]) And ($_POST["userPassword"]!='')) {
   $link = mysql_connect($host, $username, $password);
   if (!$link) {
       die('No connection: ' . mysql_error());
   }
   @mysql_select_db($db_name) or die( "Unable to select database");
   $query="SELECT password FROM $tbl_name WHERE username='".$_POST['username']."'";
   $result = mysql_query($query);
   if (!$result) {
       die('Could not query:' . mysql_error());

   }
   if (mysql_num_rows($result)>0){
      
      $dbpassword=mysql_result($result, 0);
     
      list($md5pass, $saltpass) = split(":", $dbpassword);
      if ((md5($_POST["userPassword"].$saltpass))==$md5pass) {
         //user and password are ok.
   
   //here comes the problem, even thoug that the application is working just fine, the score updates in my DB, I cant get Flash reciving information like the score, or the username or such from here   
         
mysql_query("UPDATE jos_users SET score='$score' WHERE username='$myusername'");
      
//I want a confirmation or something, anything, but NOTHING works from here.      
echo "score=$score";
   
      } else {$error = 'wrong data';}
   } else {$error = 'wrong data'';}
}


mysql_close($link);

?>


And here is the As3 code

Code: Select all



var loader:URLLoader = new URLLoader();
var variables:URLVariables = new URLVariables();
var varSend:URLRequest = new URLRequest("http://xxx.com/insert_score.php");
var varLoader:URLLoader = new URLLoader;
varSend.method = URLRequestMethod.POST;
varSend.data = variables;
var score:Number = MovieClip(parent).score;
scoreTxt.text = score.toString();
status_txt.text = "";

submit_btn.addEventListener(MouseEvent.CLICK, ValidateAndSend);
cleanBtn.addEventListener(MouseEvent.CLICK, cleanForm);
cleanBtn.buttonMode = true;
submit_btn.buttonMode = true;

function cleanForm(e:MouseEvent):void
{
   usernameTxt.text = "";
   passwordTxt.text = "";
   scoreTxt.text = MovieClip(parent).score;
   status_txt.text = "";
}

function ValidateAndSend(event:MouseEvent):void
{   
   if(!usernameTxt.length) {
      status_txt.text = "No username";   
   } else if(!passwordTxt.length) {
      status_txt.text = "No password";
   }else{
      
      
      status_txt.text = "Thank you" + usernameTxt.text + ", for your entry";
      
        variables.username = usernameTxt.text;
      variables.userPassword = passwordTxt.text;
         variables.score = MovieClip(parent).score;
         varLoader.load(varSend);
      
      loader.dataFormat = URLLoaderDataFormat.VARIABLES;
      loader.addEventListener(Event.COMPLETE, varsLoaded);
      loader.load(new URLRequest("http://xxx.com/insert_score.php"));
      
   }
}


////////////////////////////////////////////////////////////



function varsLoaded (e:Event):void {
   trace(e.target.data is URLVariables);
   trace(e.target.data);
   status_txt.text = "Nice, your score is updated =" + e.target.data.score;
   
}

I really want to know if someone can figure what is happening and How can I put this little application on track. Thanks for any information about this issue.

Greetings.

djburner
Forum Newbie
Posts: 2
Joined: Sun Dec 11, 2011 10:24 pm

Re: Using PHP cant pass a variable from MySql to Flash

Post by djburner »

Hi there, I ve seen that my problems comes from how to pass a variale from PHP tp flash. Im so meesd up with this comma things that Im pretty sure thats my problems: heres the code:

Code: Select all

$query = mysql_query("SELECT * FROM $tbl_name WHERE username='$myusername'");
      $result = mysql_fetch_assoc($query);
      $num = mysql_num_rows($query);
     
      if($num) {
         // UPDATE SCORE
         if($result['score'] < $score) {
            mysql_query("UPDATE jos_users SET score='$score' WHERE username='$myusername'");
//heres come my problem, whats the right way to write the variable below?
         
echo 'score=$result['score']';
         }
     }


I just need to have this score in flash, but I dont know how to write it right.
Hope anyone cand help me out.
Greettings.
Post Reply