Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在php mysql中从数据库中检索问题和答案_Php_Mysql - Fatal编程技术网

如何在php mysql中从数据库中检索问题和答案

如何在php mysql中从数据库中检索问题和答案,php,mysql,Php,Mysql,我正在用php做测验,有一些问题。所以我想从数据库中检索问题、答案和答案点。 例如,我有10个问题,当用户标记所有问题时,我想计算标记单选按钮的点数。我希望根据数据库值动态地执行此操作。我不想静态检索10个单选按钮的值并求和 <div class="test-data"> <form method="POST" action="index.php"> <?php foreach($test_data as $id_question => $item): ?

我正在用php做测验,有一些问题。所以我想从数据库中检索问题、答案和答案点。 例如,我有10个问题,当用户标记所有问题时,我想计算标记单选按钮的点数。我希望根据数据库值动态地执行此操作。我不想静态检索10个单选按钮的值并求和

<div class="test-data">

<form method="POST" action="index.php">

<?php foreach($test_data as $id_question => $item): ?>

<div class="question" data-id="<?=$id_question?>" id="question-<?=$id_question?>">

<?php foreach($item as $point => $answer): ?>

<?php if(!$point): ?>
    <p class="q"><?=$answer?></p>
        <?php else: ?>
            <p class="a">
                <input type="radio" id="answer-<?=$id_answer?>" name="question-<?=$id_question?>" value="<?=$point?>" /><?=$answer?>
            </p>
<?php endif; ?>

<?php endforeach; ?>

</div>

<?php endforeach; ?>

            <input type="submit" name="finish_test" id="finish_test" value="Finish" />

</form>

</div>

<?php endif; ?>

}

请添加您想要的详细信息。可能是一个精确的
用例
?请。它们不再得到维护,而是在使用。学习一下,然后考虑使用PDO,我不明白。实际的问题是什么?如果我有10个问题,我必须创建10个变量并保存这个单选按钮的值$问题1=$_POST['question1']。。等等但我想动态地做。制作一些数组,将这10个值推入该数组,然后计算该数组的和。დემე თუ მიხვდი ჩემს პრობლემას შეგიძლია სკაიპი ან ფბ მომცე და დამეხმარო?
function get_test_data(){
$query = "SELECT q.question, q.quizz_id, a.id, a.answer, a.question_id, a.point
                    FROM questions q
                    LEFT JOIN answers a
                        ON q.id = a.question_id";
$res = mysql_query($query);
$data = null;
while($row = mysql_fetch_assoc($res)){
    if(!$row['question_id']) return false;
    $data[$row['question_id']]['0'] = $row['question'];
    $data[$row['question_id']][$row['point']] = $row['answer'];
}
return $data;