douduan9129 2014-09-20 14:46
浏览 25

使用PHP和MySql的Array_combine

I have a problem with the use of the function array_combine. This is the script I am using

$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>'; 

From the database, I extract two arrays as shown above. Then when I use the array_combine nothing appears on the screen. It seems that the function does not recognize the array. I cannot understand the reason.

  • 写回答

1条回答 默认 最新

  • doz59484 2014-09-20 18:36
    关注

    This should work better

    $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>'; 
    
    评论

报告相同问题?

悬赏问题

  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事: