php - flash as3.0(flash pro)与PHP和MySql的连接

标签 php mysql flash

我知道这个问题经常出现,但我没有在互联网上的每个教程中找到答案。我不知道那里有什么错误,无论是flash与php之间的连接还是php与MySql之间的连接。 这是我的代码。

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;
         
        }
     
    }
 
}

控制面板.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.";
 
}
 
}
 
?>

连接.php

<?php
 
/*
Database vars that we use to connect to our mysql database. Change the values to your database settings.
*/
 
$database = "admin";
 
$username = "root";
 
$password = "";
 
$host = "localhost";
 
/*
mysql_connect is a built in function that allows us to make an easy connection.
*/
 
mysql_connect($host, $username, $password, $database);
 
/*
mysql_select_db is a built in function that allows us to select the database. This is an essential function.
 
We use the 'die' function to check for errors.
 
*/
 
mysql_select_db($database) or die (mysql_error());
 
echo 'success';
 
?>

在我成为数据库之前。

数据库

enter image description here

请帮我找出错误。我将非常感激并感谢您的回答。预先感谢您。

最佳答案

您需要在服务器端测试您的 PHP/MySQL 连接(只需编写一个带有静态值的 PHP 脚本,看看是否会出现错误)。对于客户端,尝试将这些事件添加到您的加载程序中,它可能会提示您出了什么问题:

    phpLoader.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
    phpLoader.addEventListener(IOErrorEvent.NETWORK_ERROR, onIOError);
    phpLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecurityError);
    phpLoader.addEventListener(HTTPStatusEvent.HTTP_STATUS, onHTTPStatus);

    private function onIOError(e:IOErrorEvent):void
    {
        trace("onIOError " + e.toString());
    }

    private function onSecurityError(e:SecurityErrorEvent):void
    {
        trace("onSecurityError " + e.toString());
    }

    private function onHTTPStatus(e:HTTPStatusEvent):void
    {
        trace("onHTTPStatus statuscode " + e.status + " - " + e.toString());
    }

关于php - flash as3.0(flash pro)与PHP和MySql的连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36685173/

相关文章:

php - 使用 HTTP-Basic 身份验证发出 HTTP GET 请求

javascript - 使用 jquery 加载隐藏的 div - 检测哪个 div

php - 删除查询(Mysql、Php)

CodeIgniter,set_flashdata 在重定向后不工作

javascript - 页面加载swf对象后添加模拟鼠标左键单击

java - 在服务器端接收值( reslet java )从客户端发送( php )

php - cron 作业正在 magento 站点中运行 - 但未找到心跳

MySQL 多表连接求和错误

php - 有没有办法在一个脚本中的一系列网页上使用 file_get_html ?

javascript - 在网页上加载 Flash 横幅后添加 cookie