dongnianwo8289 2015-03-31 06:15
浏览 129
已采纳

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

I had created a database "new" using xampp localhost database had 2 tables user and score. I had an AS3 code which had 2 input textfield to insert user and score value into database tables. Now I am trying to retrieve the inserted scores from database using user name. I have taken another text field to take user name and a button when I write "sarah" and click button it will return the score of sarah which is already inserted in database. But code is showing an error. I tried a lot but can not fix it.please help.here is my code

AS3 code:

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条回答 默认 最新

  • duanpo6079 2015-03-31 15:48
    关注

    You have some "problems" in your code, lets start by the PHP code.

    PHP code :

    <?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 code :

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

    Hope that can help.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于游戏私服的问题,找个靠谱的
  • ¥15 C#字典调用时报错,有关文件加载
  • ¥100 在连接内网VPN时,如何同时保持互联网连接
  • ¥15 MATLAB中使用parfor,矩阵Removal的有效索引在parfor循环中受限制
  • ¥20 Win 10 LTSC 1809版本如何无损提升到20H1版本
  • ¥50 win10 LTSC 虚拟键盘不弹出
  • ¥30 关于PHP中POST获取数据的问题
  • ¥30 微信小程序请求失败,网页能正常带锁访问
  • ¥15 Python实现hog特征图可视化
  • ¥30 德飞莱51单片机实现C4炸弹