为什么我会返回重复的结果?

时间:2017-02-21 12:58:36

标签: php mysql while-loop duplicates vbulletin

我开始说这对我来说是全新的,无论它们是否是高级动作,我只是将其他产品拼凑在一起并根据我的需要进行调整。重要的是,我不太明白,我知道基础知识,而且我知道我想要实现的目标是什么。

所以我现在已经玩了一段时间了,我已经设法让事情发挥作用,但我现在得到了重复的结果。

这是我的代码:

<?php
$addonid = $db->query_read("
      SELECT drc.threadid AS threadid
      FROM `" . TABLE_PREFIX . "modsys_settings` AS drc
      LEFT JOIN `" . TABLE_PREFIX . "thread` AS thread
      ON(drc.mod_addons=" . $threadinfo['threadid'] . ")
      WHERE thread.threadid IN (" . $threadinfo['threadid'] . ")
    ");
while ($addons = $db->fetch_array($addonid)) {
    $ci_counter = $db->query_read("
          SELECT drc.mod_addons AS addon, drc.threadid, thread.threadid AS threadid, thread.title AS threadtitle
          FROM `" . TABLE_PREFIX . "modsys_settings` AS drc 
          LEFT JOIN `" . TABLE_PREFIX . "thread` AS thread ON(drc.mod_addons=" . $threadinfo['threadid'] . ") 
          WHERE thread.threadid IN (" . $addons['threadid'] . ")
      ");

    while ($counter = $db->fetch_array($ci_counter)) {
        $post['addons'] .= '<li><a href="showthread.php?t=' . $addons['threadid'] . '">' . $counter['threadtitle'] . '</a></li>';
    }
}
?>

那乱糟糟的是什么!?嗯,这是我设法上班的大声笑,所以它做了什么?简单来说,它在一列中查找threadid,然后如果它在另一个表列中,则从该表中获取标题列。然后,它使用值的变量为每个列表创建一个列表链接。当它实际起作用时让我感到震惊,并不是我在这里的意思,而是可以自由地告诉我如何改进它大声笑。

问题是,它返回了所有内容的两倍,因此不是

<li><a href="showthread.php?t=4">result</a></li>
<li><a href="showthread.php?t=5">result 2</a></li>

我正在

<li><a href="showthread.php?t=4">result</a></li>
<li><a href="showthread.php?t=4">result</a></li>
<li><a href="showthread.php?t=5">result 2</a></li>
<li><a href="showthread.php?t=5">result 2</a></li>

这是为什么?我该怎么做才能解决它?

我已从循环中删除了查询,但现在它只返回它找到的第一列的重复项(即4)

<?php
$addonid = $db->query_read("
      SELECT drc.threadid AS threadid
      FROM `" . TABLE_PREFIX . "modsys_settings` AS drc
      LEFT JOIN `" . TABLE_PREFIX . "thread` AS thread
      ON(drc.mod_addons=" . $threadinfo['threadid'] . ")
      WHERE thread.threadid IN (" . $threadinfo['threadid'] . ")
    ");
$addons = $db->fetch_array($addonid);
$ci_counter = $db->query_read("
          SELECT drc.mod_addons AS addon, drc.threadid, thread.threadid AS threadid, thread.title AS threadtitle
          FROM `" . TABLE_PREFIX . "modsys_settings` AS drc 
          LEFT JOIN `" . TABLE_PREFIX . "thread` AS thread ON(drc.mod_addons=" . $threadinfo['threadid'] . ") 
          WHERE thread.threadid IN (" . $addons['threadid'] . ")
      ");

while ($counter = $db->fetch_array($ci_counter)) {
    $post['addons'] .= '<li><a href="showthread.php?t=' . $addons['threadid'] . '">' . $counter['threadtitle'] . '</a></li>';
}
?>

正在回归:

<li><a href="showthread.php?t=4">result</a></li>
<li><a href="showthread.php?t=4">result</a></li>

1 个答案:

答案 0 :(得分:2)

    $addonid = $db->query_read ("
      SELECT drc.threadid AS threadid
      FROM `" . TABLE_PREFIX . "modsys_settings` AS drc
      LEFT JOIN `" . TABLE_PREFIX . "thread` AS thread
      ON(drc.mod_addons=" . $threadinfo['threadid'] . ")
      WHERE thread.threadid IN (" . $threadinfo['threadid'] . ")
    ");
    while ($addons = $db->fetch_array ($addonid)) {    
        $ci_counter = $db->query_read ("
          SELECT drc.mod_addons AS addon, drc.threadid, thread.threadid AS threadid, thread.title AS threadtitle
          FROM `" . TABLE_PREFIX . "modsys_settings` AS drc 
          LEFT JOIN `" . TABLE_PREFIX . "thread` AS thread ON(drc.mod_addons=" . $threadinfo['threadid'] . ") 
          WHERE thread.threadid IN (" . $addons['threadid'] . ")
      ");

      $counter = $db->fetch_array ($ci_counter);
      if($counter != ''){
      $post['addons'] .= '<li><a href="showthread.php?t=' .$addons['threadid'] . '">'. $counter['threadtitle'] .'</a></li>';
      }

    }