使用as3从数据库中重定时数据


retieve data from database using as3

我使用examplep创建了一个数据库"new"。localhost数据库有两个表user和score。我有一个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());
}
?>

您的代码中有一些"问题",让我们从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);
}

希望这能有所帮助。