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 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题