将变量从数据库加载到我的游戏中的正确方法是什么?
我尝试使用 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 库是否有一些内置的方法来处理这个问题?