Skip to main content
Participant
April 29, 2011
Question

help in establishing connection to php and mysql data base.

  • April 29, 2011
  • 2 replies
  • 659 views

Hi,

i have been trying to execute a simple flash program that communicates with a php page which is linked to a mysql database.

somehow the php is not returning any data to flash , and even falsh is not sending any data to php.. I am using wamp server on win7.

Heres my actionscript code:

stop();
var variables:URLVariables = new URLVariables();
var varSend:URLRequest = new URLRequest("php.php");

varSend.method = URLRequestMethod.POST;
varSend.data = variables;

var varLoader:URLLoader = new URLLoader;
//varLoader.dataFormat = URLLoaderDataFormat.VARIABLES;
varLoader.dataFormat = URLLoaderDataFormat.TEXT;
varLoader.addEventListener(Event.COMPLETE, completeHandler);

variables.requestCode = "homepage";
//variables.myRequest="homepage";
trace(variables);

varLoader.load(varSend);


function completeHandler(event:Event):void
{
       
    trace(event.target.data.t_passed);
    var t_recieved = event.target.data.t_passed;
    //t_txt.text = "t_passed";
    t_txt.text=t_recieved;
   
   
}
try
{
    varLoader.load(varSend);
}
catch (error:Error)
{
    trace("error");
}

And this is my php code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<?php
//error_reporting(E_ALL  & ~E_NOTICE);
$connection=mysql_connect("localhost" , "root" , "");

if(!$connection)
{
    die("failed".mysql_error());

}

$db_select = mysql_select_db("questions",$connection);
if (!$db_select)
{
    die("failed".mysql_error());
}
   
    if ($_POST['requestCode'] == "homepage") {
   
    $pageName = $_POST['requestCode'];
    echo ($_POST['requestCode']);
    }
$result=mysql_query("select q from quest where qno=111",$connection);
while($row=mysql_fetch_array($result))
$t=$row[0];


//print "t_passed = $s";
echo "t_passed=".$t;


mysql_close($connection);
?>


</head>


<body>
</body>
</html>

The php gives me Undifined notice for the 'requestCode' variable...

please help..

Thanks in advance

This topic has been closed for replies.

2 replies

Inspiring
April 30, 2011

First, remove all html related tags and retest. Flash expects flat text file that contains url query string.

Deven27Author
Participant
April 30, 2011

hi Andrei !

i did remove it , but same problem ...

Deven27Author
Participant
April 29, 2011

in the URL request i have put a dynamic site ,

eg http:\\somesite.com\flash\php,php

relaxatraja
Inspiring
April 30, 2011

Sample Flash Code:

package

{

    import flash.display.*;

    import flash.events.*;

    import flash.net.*;

    import flash.text.*;

    public class sendtophp extends Sprite

    {

        var phpFile:String = "http://localhost/Raja/sendDataToServer.php";

        public function sendtophp():void

        {

            callBtn.addEventListener(MouseEvent.CLICK, callServer);

        }

        function callServer(e:MouseEvent):void

        {

            var urlRequest:URLRequest = new URLRequest(phpFile);

            var urlParams:URLVariables = new URLVariables();

            urlParams.country = "India";

            urlParams.animal="Tiger";

            urlRequest.method = URLRequestMethod.POST;

            urlRequest.data = urlParams;

            var loader:URLLoader = new URLLoader();

            loader.addEventListener(Event.COMPLETE, serverResponse);

            loader.load(urlRequest);

        }

        function serverResponse(e:Event):void

        {

            trace("loaded");

            var loader:URLLoader = URLLoader(e.target);

            var variables:URLVariables = new URLVariables(loader.data);

            responseTxt.text = variables.returnValue;

        }

    }//end of class sendtophp

}//end of package

Sample PHP code:

<?php

print "returnValue= Hi ".$_POST['country'].", it's working";
    $host = "localhost";
    $user = "root";
    $pass = "";
    $db = "test";
   
    $country = $_POST['country'];
    $animal = $_POST['animal'];
   
     $connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");
    
     mysql_select_db($db) or die ("Unable to select database!");
    
      $query = "INSERT INTO symbols (country, animal) VALUES ('$country', '$animal')";
     
      $result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());
    echo "New record inserted with ID ".mysql_insert_id();
       mysql_close($connection);
?>

I had tested and it works fine. Please have this as an sample. If you have any clarification then reply to the thread