Flash Cs3,flex and PHP communication

Source: Internet
Author: User
Tags mysql connect php code

Flash and PHP:
First put two code:
Flash
var loader:urlloader = new Urlloader ();

var url:http://www.111cn.net/flash_a/114/urlrequest = new Http://www.111cn.net/flash_a/114/URLRequest ("test.php");

Url.method = Http://www.111cn.net/flash_a/114/URLRequestMethod.POST;

var values:urlvariables = new Urlvariables ();

values.message= "Hello im flash!";
Url.data = values;

Loader.dataformat = Urlloaderdataformat.variables;
Loader.addeventlistener (event.complete,loaded);
function loaded (e:event) {
Out1.text=loader.data;
Output:phpmessage=im php Message!&flashmessage=hello im flash!
Out2.text=loader.data.phpmessage;
im PHP message!
Out3.text=loader.data.flashmessage;
Hello im flash!
Out4.text=loader.data.dbmessage;
}
Loader.load (URL);
Php:
<?php
This is all about database operations.
//===================================
Define (' database_server ', ' localhost ');
Define (' Database_username ', ' root ');
Define (' Database_password ', ' root ');
Define (' database_name ', ' flextest ');
$link = @mysql_connect (Database_server,database_username,database_password);
@mysql_select_db (database_name, $link);
$result = @mysql_query ("Select * from users");
$user = @mysql_fetch_array ($result, MYSQL_ASSOC);
echo $user [' userid '];
$dbvar = $user [' userid '];
//===================================
Receive the data that flash passes in
$flashData = $_post[' message '];
Data sent to Flash
$phpMessage = ' im php message! ';
echo "phpmessage= $phpMessage &flashmessage= $flashData &dbmessage= $dbvar";
?>
The code is very simple, do not need to explain, in fact, and the ASP is not much different. Directly copied to the path of PHP can be tested. The database file executes the SQL statements in Flextest.sql directly under phpMyAdmin. Because the company banned the port, I can't operate my space SQL database online, online demo address is not, and so have time to add at home.

Flex vs php:
First put two code:
Flex:
<?xml version= "1.0" encoding= "Utf-8"?>
<mx:application xmlns:mx= "Http://www.adobe.com/2006/mxml" creationcomplete= onInit () "xmlns=" * "layout=" Absolute "backgroundgradientcolors=" [#ffffff, #c0c0c0] ">
<mx:Script>
<! [cdata[
Public Function OnInit (): void
{
Userrequest.send ();
}
]]>
</mx:Script>
<mx:httpservice id= "Userrequest" url= "request.php" useproxy= "false" method= "POST" >
<mx:request xmlns= "" >
<username>{username.text}</username><emailaddress>{emailaddress.text}</emailaddress>
</mx:request>
</mx:HTTPService>
<mx:form x= "y=" width= "356" >
<mx:HBox>
<mx:label text= "Username"/>
<mx:textinput id= "username"/>
</mx:HBox>
<mx:HBox>
<mx:label text= "Email address"/>
<mx:textinput id= "EmailAddress"/>
</mx:HBox>
<mx:button label= "Submit" click= "userrequest.send ()"/>
</mx:Form>
<mx:datagrid id= "dguserrequest" x= y= "128" dataprovider= "{UserRequest.lastResult.users.user}" >
<mx:columns>
<mx:datagridcolumn headertext= "User ID" datafield= "userid"/>
<mx:datagridcolumn headertext= "User Name" datafield= "username"/>
</mx:columns>
</mx:DataGrid>
<mx:textinput x= y= "292" id= "selectedemailaddress" text= "{dgUserRequest.selectedItem.emailaddress}"/>
</mx:Application>

php:<?php
/* To Pete Mackie for the code below * *

Define (' database_server ', ' localhost ');
Define (' Database_username ', ' root ');
Define (' Database_password ', ' root ');
Define (' database_name ', ' flextest ');

# Connect to the database
$mysqli = new Mysqli (Database_server, Database_username, Database_password, database_name);

# Check Connection
if (Mysqli_connect_errno ()) {
printf ("MySQL Connect failed:%s\n", Mysqli_connect_error ());
Exit ();
}

# Quote variable to make safe
function Quote_smart ($value) {
Global $mysqli;
# stripslashes
if (GET_MAGIC_QUOTES_GPC ())
$value = Stripslashes ($value);

# Quote if not integer
if (!is_numeric ($value))
$value = $mysqli->real_escape_string ($value);
return $value;
}

if (!empty ($_post) && $_server[' request_method '] = = ' POST ') {
if ($_post[' EmailAddress '] && $_post[' username ') {
# ADD the user
$query = sprintf (Insert into Users VALUES (', '%s ', '%s '), Quote_smart ($_post[' username '), Quote_smart ($_post[' EmailAddress ']));
if (!@ $mysqli->query ($query)) {
printf ("' flextest ' user database query Insert error:%s\n", $mysqli->error);
$mysqli->close ();
Exit ();
}
}
}

# return a list of all users
if (! $result =@ $mysqli->query ("SELECT * from users")) {
printf ("' flextest ' user database query Select error:%s\n", $mysqli->error);
$mysqli->close ();
Exit ();
}

$xml _return = "<users>";
while ($user = Mysqli_fetch_array ($result, Mysqli_assoc)) {
        $xml _ return. =
        "<user><userid>" $user [' userid ']. " </userid><username> ". $user [' username ']." </username><emailaddress> ". $user [' EmailAddress ']." </emailaddress></user>\n ";
}
$xml _return.= "</users>";
$mysqli->close ();
Echo $xml _return;
?
The code here is an instance of the Adobe Phpria SDK. If you want all the source code, please leave the mailbox, I will send. Flex and PHP will not say, directly to the PHP code into ASP code on the line, and flash of the truth.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.