与LimeJS中的数据库通信

时间:2013-07-05 03:54:50

标签: database limejs

从数据库中将变量加载到我的游戏中的正确方法是什么?

我尝试使用Ajax和Prototype库,但这似乎不起作用。这是我做的:

在我的主要.js游戏文件中......

var rawVocab = new Array();
var optionVocab = new Array();

new Ajax.Request('load_vocab.php', {
    onSuccess : function(xmlhttp) {
        eval(xmlhttp.responseText);
    }
});

'load_vocab.php'看起来像这样......

<?php

header('Content-Type: text/xml');
echo '<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>';

echo '<response>';

    $username = "user";
    $password = "***************";

    try {
        $conn = new PDO('mysql:host=localhost;dbname=tygrif_school', $username, $password);
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

        $stmt = $conn->prepare('SELECT * FROM vocabulary_game WHERE game_type = :game_type');
        $stmt->execute(array('game_type' => 'target'));

        $i=0;
        while($row = $stmt->fetch()) {
            echo "rawVocab[".$i."]['word']='".$row['word']."';";
            echo "rawVocab[".$i."]['translation']='".$row['translation']."';";
            echo "rawVocab[".$i."]['example_sentence_1']='".$row['example_sentence_1']."';";
            $i++;
        }

        $stmt = $conn->prepare('SELECT * FROM vocabulary_game');
        $stmt->execute(array());

        $i=0;
        while($row = $stmt->fetch()) {
            echo "optionVocab[".$i."]['word']='".$row['word']."';";
            echo "optionVocab[".$i."]['translation']='".$row['translation']."';";
            echo "optionVocab[".$i."]['example_sentence_1']='".$row['example_sentence_1']."';";
            $i++;
        }
    } catch(PDOException $e) {
        echo 'ERROR: ' . $e->getMessage();
    }

echo '</response>';

?>

是否有一些内置方法可以使用goog库来处理它?<​​/ p>

1 个答案:

答案 0 :(得分:0)