使用as3从数据库中检索数据

时间:2015-03-31 06:15:43

标签: php database actionscript-3 flash mysqli

我创建了一个数据库“new”,使用xampp localhost数据库有2个表用户和得分。我有一个AS3代码,它有2个输入文本字段,用于将用户和分数值插入数据库表。 现在我尝试使用用户名从数据库中检索插入的分数。当我写“sarah”并点击按钮时,我已经取了另一个文本字段来取用户名和一个按钮,它将返回已经插入数据库中的sarah的分数。但代码显示错误。我尝试了很多,但无法修复它。请帮助。我的代码是

AS3代码:

btn2.addEventListener(MouseEvent.MOUSE_DOWN, fetchscore);

function fetchscore(event:MouseEvent)
{
    var phpVars:URLVariables = new URLVariables();

    var phpFileRequest:URLRequest = new URLRequest('http://localhost/collectscore.php');

    phpFileRequest.method = URLRequestMethod.POST;

    var phpLoader:URLLoader = new URLLoader();
    phpLoader.dataFormat = URLLoaderDataFormat.VARIABLES;           
    phpLoader.addEventListener(Event.COMPLETE, showResult);

    phpVars.systemCall = "processLogin";
    phpVars.cname = Name.text;

    phpLoader.load(phpFileRequest);
}

function showResult(e:Event)
{
    //trace(phpVars.result);
    result_text.text = "" + e.target.data.systemResult;
}

fetchscore.php

<?php 
 include('connect.php');
 $username = $_POST['cname'];

if ($_POST['systemCall'] == "processLogin"){
$sqlqry = "SELECT * FROM scoreu WHERE username='$username'";//scoreu is my DBtable with two field user and score 
$query = mysqli_query($sqlqry);

$login_counter = mysqli_num_rows($query);

if ($login_counter > 0) {
while ($data = mysqli_fetch_array($query)) {
if (mysqli_query($link), "SELECT score FROM scoreu WHERE user='$username'")) {
$findscore = $data['score'];
print 'result=$findscore';
}
}
} else {
 print 'result=The login details dont match names.';
}
}
?>

connect.php

<?php

// connect.php

$db_name = 'new';
$db_username = 'root';
$db_password = '';
$db_host = 'localhost';

$link = mysqli_connect($db_host, $db_username, $db_password, $db_name);
if (mysqli_connect_errno()) {
    die('Failed to connect to the server : '.mysqli_connect_error());
}
?>

1 个答案:

答案 0 :(得分:0)

你有一些&#34;问题&#34;在您的代码中,让我们从PHP代码开始。

PHP代码:

<?php

    // collectscore.php

    include('connect.php');

    $username = $_POST['cname'];
    if ($_POST['systemCall'] == "processLogin"){
        // you need only one request to get the score
        $query = mysqli_query($link, "SELECT score FROM scoreu WHERE user = '$username'");
        // you have to fetch the result into a php var
        if ($data =  mysqli_fetch_assoc($query)) {
            $findscore = $data['score'];
            // you should use double quotes when passing vars into a string
            // otherwise, if you want use single quotes, you can write it : print 'result='.$findscore;
            print "result=$findscore";
        } else {
            print 'result=The login details dont match names.';
        }
    }

?>

ActionScript代码:

function fetchscore(event:MouseEvent): void
{
    var phpVars:URLVariables = new URLVariables();
        phpVars.systemCall = "processLogin";
        phpVars.cname = cname.text; // name text field 

    var phpFileRequest:URLRequest = new URLRequest('http://127.0.0.1/collectscore.php');
        phpFileRequest.method = URLRequestMethod.POST;
        // you forgot to send your POST vars, for that, we use URLRequest.data
        phpFileRequest.data = phpVars;

    var phpLoader:URLLoader = new URLLoader();
        phpLoader.dataFormat = URLLoaderDataFormat.VARIABLES;
        phpLoader.addEventListener(Event.COMPLETE, showResult);
        phpLoader.load(phpFileRequest);
}
function showResult(e:Event):void 
{
    // here the var should be the same as in the PHP script, result in this case : print "result=$findscore";
    trace(e.target.data.result);
}

希望可以提供帮助。