从PDO数据库获取结果

问题描述:

我开发了一个简单的上传照片和评论系统。每次使用时,都会重复该图像。例如,如果在照片上有5条评论。它用5个不同的评论重复5次照片。 5个评论,而不是一个图像。从PDO数据库获取结果

我想要每个评论1个图像。如何在不重复照片的情况下获取每张照片的所有评论?

我使用PDO这里是PHP代码:

<?php 
//This is for the images 
$results = $connecDB->prepare("select image,caption,id from gallery order by id desc LIMIT $start, $limit "); 
$results->execute(); 
$results = $results->fetchAll(); 

foreach($results as $results) { 
    $pid=$results["id"]; 

    //This is for the comments 
    $re= $connecDB->prepare("select * from comments where pid = :pid order by id"); 
    $re->bindParam(':pid', $pid); 
    $re->execute(); 

    foreach($re as $re) { 
     $name=$re["name"]; 
     $comment=$re["comment"]; 
?> 

<div class="item" id="item-<?php echo $results['id']?>"> 
    <p><img src="../upload/images/<?php echo $results['image']?>" height="500px" width="500px"</p> 
    <p><?php echo $results['caption']?></p> 
    <div class="large-2 columns small-3"><font color="#3366ff"><?php echo $name?></font></div> 
    <div class="large-10 columns"><p><?php echo $comment?></p></div> 
<?php} 
}?> 

感谢。

+0

你可以在这里附上你的输出吗?也许是输出的图像? – Akshay

+0

因为您正在覆盖最初的'$ results'变量,所以不能执行'foreach($ results as $ results)'。应该是'foreach($ results as $ result)',然后在块内部使用'$ result ['id']'等。解决这个问题,看看你留下了什么。 (与'foreach($ re为$ re)'块一样...) – patricus

+0

检查答案。 –

你是循环里面的所有东西内部循环,试试这个。

<?php 
//This is for the images 
$results = $connecDB->prepare("select image,caption,id from gallery order by id desc LIMIT $start, $limit "); 
$results->execute(); 
$results = $results->fetchAll(); 

foreach($results as $results) { 
    $pid=$results["id"]; 
?> 
    <div class="item" id="item-<?php echo $results['id']?>"> 
     <p><img src="../upload/images/<?php echo $results['image']?>" height="500px" width="500px" /></p> 
     <p><?php echo $results['caption']?></p> 
    <?php 
     //This is for the comments 
     $re= $connecDB->prepare("select * from comments where pid = :pid order by id"); 
     $re->bindParam(':pid', $pid); 
     $re->execute(); 

     foreach($re as $re) { 
      $name=$re["name"]; 
      $comment=$re["comment"]; 
    ?> 
    <div class="large-2 columns small-3"><font color="#3366ff"><?php echo $name?></font></div> 
    <div class="large-10 columns"><p><?php echo $comment?></p></div> 
    <?php}?> 
    </div> 
<?php}?> 
+0

像魔术一样工作。谢谢。 – Ibrahim

+0

不客气 –

其实你没有得到$ re变量的结果。执行查询后,您需要获取fetchAll数据。以及你在循环内在的每一件事。

foreach($results as $results) { 
$pid=$results["id"]; 

//This is for the comments 
$re= $connecDB->prepare("select * from comments where pid = :pid order by id"); 
$re->bindParam(':pid', $pid); 
$re->execute(); 
$comments = $re->fetchAll(); 

<div class="item" id="item-<?php echo $results['id']?>"> 
    <p><img src="../upload/images/<?php echo $results['image']?>" height="500px" width="500px"</p> 
    <p><?php echo $results['caption']?></p> 
    <div class="large-2 columns small-3"><font color="#3366ff"><?php echo $name?></font></div> 
    foreach($comments as $re) { 
    $name=$re["name"]; 
    $comment=$re["comment"]; 
?> 
    <div class="large-10 columns"><p><?php echo $comment; ?></p></div> 
<?php} 
}?>