0
この質問は頻繁に起こりますが、インターネット上のすべてのチュートリアルで答えが見つかりませんでした。私はそこに間違いがあるかどうか、MySQLとのPHPまたはPHPとフラッシュの間の接続かどうかわからない。 これは私のコードです。PHPとMySqlのflash as3.0(flash pro)との接続
main.as
package actions {
/*
always extend a class using movieclip instead of sprite when using flash.
*/
import flash.display.MovieClip;
import flash.events.*;
import flash.net.*;
import flash.text.*;
/*
create our class
*/
public class main extends MovieClip {
public function main():void {
/*
buttonMode gives the submit button a rollover
*/
submit_button.buttonMode = true;
/*
what this says is that when our button is pressed, the checkLogin function will run
*/
submit_button.addEventListener(MouseEvent.MOUSE_DOWN, checkLogin);
/*
set the initial textfield values
*/
username.text = "";
pass.text = "";
}
/*
the function to check login
*/
public function checkLogin (e:MouseEvent):void {
/*
check fields before sending request to php
*/
if (username.text == "" || pass.text == "") {
/*
if username or password fields are empty set error messages
*/
if (username.text == "") {
username.text = "Enter your username";
}
if (pass.text == "") {
pass.text = "Enter your password";
}
} else {
/*
init function to process login
*/
processLogin();
}
}
/*
function to process our login
*/
public function processLogin():void {
/*
variables that we send to the php file
*/
var phpVars:URLVariables = new URLVariables();
/*
we create a URLRequest variable. This gets the php file path.
*/
var phpFileRequest:URLRequest = new URLRequest("php/controlpanel.php");
/*
this allows us to use the post function in php
*/
phpFileRequest.method = URLRequestMethod.POST;
/*
attach the php variables to the URLRequest
*/
phpFileRequest.data = phpVars;
/*
create a new loader to load and send our urlrequest
*/
var phpLoader:URLLoader = new URLLoader();
phpLoader.dataFormat = URLLoaderDataFormat.VARIABLES;
phpLoader.addEventListener(Event.COMPLETE, showResult);
/*
now lets create the variables to send to the php file
*/
phpVars.systemCall = "checkLogin";
phpVars.username = username.text;
phpVars.pass = pass.text;
/*
this will start the communication between flash and php
*/
phpLoader.load(phpFileRequest);
}
/*
function to show the result of the login
*/
public function showResult (event:Event):void {
/*
this autosizes the text field
***** You will need to import flash's text classes. You can do this by:
import flash.text.*;
*/
result_text.autoSize = TextFieldAutoSize.LEFT;
/*
this gets the output and displays it in the result text field
*/
var resultVar:URLLoader = new URLLoader(event.target.data.systemResult);
result_text.text = ""+resultVar;
}
}
}
controlpanel.php
<?php
/*
connect to our database
*/
include_once "connect.php";
/*
we post the variables we recieve from flash
*/
$username = $_POST['username'];
$pass = $_POST['pass'];
/*
if flash called the login system the code below runs
*/
if ($_POST['systemCall'] == "checkLogin") {
/*
The * means the query initally selects all the records in the database.
Then the query filters out any records that are not the same as what the user entered.
*/
$sql = "SELECT * FROM users WHERE username='$username' AND password='$pass'";
$query = mysql_query($sql);
/*
This counts how many records match our query
*/
$login_counter = mysql_num_rows($query);
/*
if $login_counter is greater we send a message back to flash and get the data from the database
*/
if ($login_counter > 0) {
/*
we use a while loop to get the user's bio. mysql_fetch_array gets all the field's data for the particular record.
*/
while ($data = mysql_fetch_array($query)) {
/*
gets the user_bio value from the selected record
*/
$userbio = $data["user_bio"];
/*
use the print function to send values back to flash
*/
print "systemResult=$userbio";
}
} else {
print "systemResult=The login details dont match our records.";
}
}
?>
connect.php
その前
私は、データベースを作りました。
データベース
は、障害を見つけるために私を助けてください。私は非常に感謝し、あなたの答えに感謝します。前もって感謝します。
私は自分の問題を知っています。私は変数をPHPからフラッシュに持っていくことはできません。 php "systemResult = '$ userbio'"; と点滅 result_text.text = event.target.data.systemResult; フラッシュで特定のデータをPHPで取得する手助けはできますか? –
ああ、今私が参照してください:)あなたのshowResultメソッドでは、それはresult_text.text = event.target.data.systemResultでなければなりません。 PHPスクリプトが印刷しているすべての変数は、event.target.dataオブジェクトの内部にあります。つまり、あなたのPHPは 'systemResult =これは私の結果です&moreInfo =もう少し情報です'と表示し、データオブジェクトにsystemResultとmoreInfoの両方の変数を持たせることができます。値が(&が&になるように)エンティティを変換する必要があることを覚えておいてください。そうでなければ、値に "&"が含まれていれば出力を中断します。 PHPスクリプトには$ userbio = htmlentities($ data ["user_bio"])のようなものが必要です。 – Philarmon