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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀