3
我現在有一個腦屁。我從數據庫中提取問題和可能的答案,以動態生成使用PHP和MySQL的測驗。下面是輸出我得到:將多行SQL查詢推送到單個PHP數組項目
Response id: 3 Question id: 1 Question: What is my middle name? Title: How Well Do You Know Michael
Array (
[0] => Array (
[r_id] => 3
[question_id] => 1
[question] => What is my middle name?
[title] => How Well Do You Know Michael))
Array (
[0] => Array (
[0] => 1
[1] => Abe)
[1] => Array (
[0] => 2
[1] => Andrew)
[2] => Array (
[0] => 3
[1] => Andre)
[3] => Array (
[0] => 4
[1] => Anderson))
從這個PHP腳本:
// Grab the question data from the database to generate the form
$query = "SELECT qr.response_id AS r_id, qr.question_id, q.question, quiz.title " .
"FROM quiz_response AS qr " .
"INNER JOIN question AS q USING (question_id) " .
"INNER JOIN quiz USING (quiz_id) " .
"WHERE qr.user_id = '" . $_SESSION['user_id'] . "'";
$data = mysqli_query($dbc, $query) or die("MySQL error: " . mysqli_error($dbc) . "<hr>\nQuery: $query");
$questions = array();
while ($row = mysqli_fetch_array($data, MYSQL_ASSOC)) {
echo 'Response id: ' . $row['r_id'] . 'Question id: ' . $row['question_id'] . ' Question: ' . $row['question'] . ' Title: ' . $row['title'] . '<br />';
array_push($questions, $row);
// Pull up the choices for each question
$query2 = "SELECT choice_id, choice FROM question_choice " .
"WHERE question_id = '" . $row['question_id'] . "'";
$data2 = mysqli_query($dbc, $query2);
$choices = array();
while ($row2 = mysqli_fetch_array($data2, MYSQL_NUM))
array_push($choices, $row2);
}
print_r($questions);
print_r($choices);
不過,我會非常喜歡有$選擇數組中只有一個項目,而不是4個獨立的項目。例如,我想要的選擇數組如下所示:
Array (
[0] => Array (
[0] => 1
[1] => Abe
[2] => 2
[3] => Andrew
[4] => 3
[5] => Andre
[6] => 4
[7] => Anderson)
我的問題:**雖然我已經從'
while ($row2 = mysqli_fetch_array($data2, MYSQL_NUM))
array_push($choices, $row2);
四個獨立的行`反正是有?將它們全部推到數組$選擇的同一項目**