使用PHP和MySql的Array_combine

时间:2014-09-20 14:46:23

标签: php

我在使用函数array_combine时遇到问题。 这是我正在使用的脚本

$questionSQL = mysql_query("SELECT * FROM tbl_questions ORDER BY id ASC");
$questions = array();
while($row = mysql_fetch_array($questionSQL)){
$int_q = $row['question'];
$questions[] = $int_q;
}

$answerSQL = mysql_query("SELECT * FROM tbl_answers ORDER BY id DESC");
$answers = array();
while($row = mysql_fetch_array($answerSQL)){
$int_a = $row['answer'];
$answers[] = $int_a;
}


echo '<div class="interviewBox">';
foreach(array_combine($questions, $answers) as $question => $answer) {
echo'

<p><b>'.questionName($question).'</b></p>
<p style="margin-bottom:20px;"><u>Answer:</u><br />
'.$answer.'
</p>';
}
echo '</div>'; 

从数据库中,我提取了两个数组,如上所示。然后,当我使用array_combine时,屏幕上不会显示任何内容。似乎该函数无法识别该数组。我无法理解原因。

1 个答案:

答案 0 :(得分:0)

这应该效果更好

$questions_and_answers = array();
$questions_and_answers_SQL = mysql_query("
    SELECT tbl_questions.questions, tbl_answers.answers 
    FROM tbl_questions 
    JOIN tbl_answers ON tbl_answers.id = tbl_questions.id
    ORDER BY tbl_questions.id ASC");
while($row = mysql_fetch_array($questions_and_answers_SQL)){
    $questions_and_answers[$row['question']] = $row['answer'];
}


echo '<div class="interviewBox">';
foreach(questions_and_answers as $question => $answer) {
echo'

<p><b>'.questionName($question).'</b></p>
<p style="margin-bottom:20px;"><u>Answer:</u><br />
'.$answer.'
</p>';
}
echo '</div>';