我想回显从json对象中从数据库中提取的php字符串。 请问是否可以,如果是的话,我在哪里弄错了? 以下是代码: 其余的效果很好。
<script type='text/javascript'>
(function() {
<?php
$sql = "SELECT * FROM quiz WHERE subject='SOCIAL STUDIES' AND type='challenge'";
$results = $pdo->query($sql);
$results->setFetchMode(PDO::FETCH_ASSOC);
while($rows = $results->fetch()){
?>
var questions = [{
question: "<?php echo $rows['text'];?>",
choices: [2, 5, 10, 15, 20],
correctAnswer: 2
},
<?php
}
?>
}];
答案 0 :(得分:0)
您可以尝试
对于json格式,您应该使用json_encode
$sql = "SELECT * FROM quiz WHERE subject='SOCIAL STUDIES' AND type='challenge'";
$results = $pdo->query($sql);
$results->setFetchMode(PDO::FETCH_ASSOC);
while($rows = $results->fetch()){
$q = $rows['text'];
$result[] = array(
"question" => $q,
"choices" => [2, 5, 10, 15, 20],
"correctAnswer" => 2
);
}
$arrRecord['data']['success'] = $result;
echo json_encode($arrRecord);