【问题标题】:avoid duplicate looping in PHP SQL避免 PHP SQL 中的重复循环
【发布时间】:2013-06-27 08:46:38
【问题描述】:

我的代码有问题,结果有一些重复循环。

这是我的代码:

 <form action="<?php $_SERVER['PHP_SELF'] ?>" method="post" data-iceapw="6">
 <?php for($a=0;$a<5;$a++) {
$sql = "SELECT * FROM personality_question_tbl where personality_question_id in (select personality_question_id from
                                                                        personality_question_type_tbl where
                                                                        personality_id='P01' ) ORDER BY RAND() limit 1" ;
$result = mysql_query($sql); 
while ($row = mysql_fetch_array($result)) {
    extract ($row);
    }
?> 


<table width="200" border="0">
  <tr>
    <td><?php echo $personality_question_id[$a]; ?> </td>
  </tr>
  <tr>
    <td>
      <table width="200">
        <tr>
          <td><label>
            <input type="radio" name="answer" value="1" id="answer_0">
            yes</label></td>
        </tr>
        <tr>
          <td><label>
            <input type="radio" name="answer" value="0" id="answer_1">
            no</label></td>
        </tr>
      </table>
    </td>
  </tr>
 </table>
 </form>
<?php } ?>
<input name="submit" type="submit" value="Go">
<input name="submitted" type="hidden" value="TRUE">
</form>

问题是我仍然在PHP 中得到重复的结果,尽管数据库中没有重复的数据...

【问题讨论】:

  • 您可以尝试将GROUP BY personality_question_id 添加到您的查询中。就在ORDER BY 之前

标签: php sql loops for-loop


【解决方案1】:

你做一个 for 循环,当然你会有重复.. 除非我不理解你的问题

删除&lt;?php for($a=0;$a&lt;5;$a++) {,您将解决您的重复问题

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多