SQL查询结果重复

凯里多

PHP新手在这里!我已经为此苦苦挣扎了几天,我已经决定自己不能解决这个问题。

基本上我有2个数据库表“ projects_2016”和“ attachment”。

我想显示“ projects_2016”的数据以显示在顶部表格中,然后在“附件”中检查匹配的ID号(如果存在),它将在“ project_2016数据”下列出所有结果。

目前,它的效果很好,但是它为每个“附件”条目都复制了“ projects_2016”数据。

这是我的代码,感谢您的任何投入!

PS不太关心Sql注入。还在学习!

<?php include '../../../connection_config.php'; 

$sql = "SELECT DISTINCT * FROM attachment JOIN projects_2016 ON             attachment.attachment_ABE_project_number = projects_2016.id ORDER BY `attachment_ABE_project_number` DESC";

$result = $conn->query($sql);

if ($result->num_rows > 0) {

while($row = $result->fetch_assoc()) {

?>

<table width="20" border="1" cellspacing="0" cellpadding="2">
<tr>
<th height="0" scope="col"><table width="990" border="0" align= "center" cellpadding="3" cellspacing="0">
        <tr class="text_report">
          <td width="107" height="30" align="left" valign="middle" nowrap="nowrap" bgcolor="#F5F5F5"><strong>PNo</strong></td>
          <td width="871" align="left" valign="middle" nowrap="nowrap" bgcolor="#F5F5F5"><strong>Project Name</strong></td>
        </tr>
        <tr>
          <td height="20" align="left" valign="middle" bgcolor="#FFFFFF" class="text_report"><strong><?php echo "<br>". $row["ID"]. "<br>";?></strong></td>
          <td height="20" align="left" valign="middle" bgcolor= "#FFFFFF" class="text_report"><strong><?php echo "<br>". $row["project_name"]. "<br>";?></strong></td>
        </tr>
      </table>
        <?php  

$photo_id = $row["ID"];

$contacts = "SELECT DISTINCT * FROM attachment WHERE attachment_ABE_project_number = '$photo_id'" ;

$result_contacts = $conn->query($contacts);

if ($result_contacts->num_rows > 0) {
 // output data of each row
 while($row_contacts = $result_contacts->fetch_assoc()) {

?>
        <table width="990" border="0" align="center" cellpadding=   "3" cellspacing="0" class="text_report">
          <tr>
            <td height="0" colspan="4" align="left" valign="middle" nowrap="nowrap" bgcolor="#FFFFFF">&nbsp;</td>
          </tr>
          <tr>
            <td width="319" height="30" align="left" valign="middle" nowrap="nowrap" bgcolor="#F5F5F5"><strong>File Name</strong></td>
            <td width="279" align="left" valign="middle" nowrap="nowrap" bgcolor="#F5F5F5"><strong>File Type</strong></td>
            <td width="315" align="left" valign="middle" nowrap="nowrap" bgcolor="#F5F5F5"><strong>File Size (KB)</strong></td>
            <td width="53" align="right" valign="middle" nowrap="nowrap" bgcolor="#F5F5F5"><strong>View File</strong></td>
          </tr>
          <tr>
            <td height="20" align="left" valign="middle" bgcolor="#FFFFFF"><?php echo $row_contacts ['file'] ?></td>
            <td height="20" align="left" valign="middle" bgcolor="#FFFFFF"><?php echo $row_contacts ['type'] ?></td>
            <td height="20" align="left" valign="middle" bgcolor="#FFFFFF"><?php echo $row_contacts ['size'] ?></td>
            <td align="right" valign="middle" bgcolor="#FFFFFF"><a href="/../gauteng/uploads/<?php echo $row_contacts ['file'] ?>" target="_blank">view file</a></td>
          </tr>
          <tr>
            <td height="0" colspan="4" align="left" valign="middle" bgcolor="#FFFFFF">&nbsp;</td>
          </tr>
          <?php
}
?>
        </table>
        <?php        



}
?></th>
    </tr>
  </table>
  <table width="1000" border="0" cellspacing="0" cellpadding="0">
    <tr>
      <th height="26"> </th>
    </tr>
  </table>
  <p>
    <?php        


 }

 }
?>
  </p>
</table> 

<?php   $conn->close();
?>
凯里多

$ sql =“选择*从projects_2016存在的地方(选择*从附件WHERE projects_2016.id =附件_ABE_project_number)按idDESC排序”;

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章