echo只返回数据库中的一个结果

时间:2017-04-26 04:36:22

标签: php

我有2个数据库表用户和图库,如果用户有一个将在其下显示的图库,该页面将显示无限的个人资料。

目前,DB用户1中有2个用户有5个图库用户2没有用户。

两个表中的用户和图库表都由D = ID字段连接。

第一个用户在他的个人资料下面显示他的画廊但是用户2根本没有显示,每个新用户和画廊如果他们有一个就像列表一样在彼此之下显示。但它回应了第一个用户和他的画廊,就是这样......

如果我在WHERE U.ID = 1或2的查询中的WHERE U.ID = U.ID,它将在列表中同时显示两个用户..

CREATE TABLE `users` (
  `ID` int(11) UNSIGNED NOT NULL,
  `Email` varchar(255) NOT NULL,
  `FullName` varchar(300) NOT NULL,
  `JobTitle` varchar(300) NOT NULL,
  `Bio` text,
  `Photo` text,
) ENGINE=MyISAM DEFAULT CHARSET=utf8;


CREATE TABLE `gallery` (
  `GalleryID` bigint(11) UNSIGNED NOT NULL,
  `ID` int(11) DEFAULT NULL,
  `GalleryImage` text NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;




<?php 


$stmt = $db->prepare("
SELECT U.ID,
       U.FullName,
       U.Email,
       U.JobTitle,
       U.Bio,
       U.Photo, 
       group_concat(G.GalleryImage) as GalleryImage 
FROM users as U 
join gallery as G 
ON U.ID = G.ID 
WHERE U.ID = U.ID 

 ");


$stmt->execute();
$result = $stmt->get_result();


 while ($row = mysqli_fetch_assoc($result)) {

        $ID = $row['ID'];        
        $FullName = $row['FullName'];    
        $Email = $row['Email'];   
        $JobTitle = $row['JobTitle'];
        $Bio = $row['Bio'];
        $Photo = $row['Photo'];         
        $GalleryImage = explode(',', $row['GalleryImage']);

        if (isset($Photo) && ! empty($Photo)) {
            $ProfileImage = "$Photo";
        } else {
            $ProfileImage= "avatar.jpg";
        }


   echo "<div class='container team-wrap'>
           <div class='row'>
             <div class='col-md-6'>
             <img class='img-responsive' src='$ProfileImage'>
               </div>
                 <div class=\"col-md-6\">
                    <strong>$FullName<br>$JobTitle</strong>
                      <br>
                      <p>$Bio</p>
                      <a href='mailto:$Email' class='btn btn-info'>Contact Me</a>
                </div>
             </div>
          </div>";

     echo "

  <br>
<br>
<br>   

";

   echo "<div class='container team-wrap'>";   

   foreach ($GalleryImage as $img){        
       if (isset($img) && ! empty($img)) { 

           //Display this row as many times as needed by data in this row.
           echo "<img style='height: 100px; width: 100px;' src='cdn/assets/gallery/$img'>";       

       } else { 

           echo "NO IMAGES";
       }

   }


}  

echo"</div>";
?>

4 个答案:

答案 0 :(得分:3)

data.table

答案 1 :(得分:1)

在代码中尝试使用此SQL。

SELECT u.ID, u.Email, u.FullName, u.JobTitle, u.Bio, u.Photo,GROUP_CONCAT(g.GalleryImage) GalleryImage
FROM `users` u, `gallery` g 
WHERE u.ID = g.ID GROUP BY u.ID

答案 2 :(得分:0)

您需要将表格的名称放在 WHERE 中的表格之前,如:

$stmt = $connection->prepare("SELECT * FROM Games, Mods WHERE Games.GameID = Mods.GameID");

这是为了识别查询中两个表的模糊(重复)名称....

我建议使用关键字JOIN作为表的关系..

$stmt = $connection->prepare("SELECT * FROM Games JOIN Mods ON Games.GameID = Mods.GameID");

在您的情况下,您可以使用函数 USING(),因为这两个表具有相同的列名,例如:

$stmt = $connection->prepare("SELECT * FROM Games JOIN Mods USING(GameID)");

答案 3 :(得分:0)

使用JOINGroup_concat您将获得逗号分隔的Modname,例如mod1,mod2,..

SELECT G.GameID,
       G.GameName,
       G.GameCover,
       group_concat(M.ModName) as ModName 
FROM Games as G 
join Mods as M 
ON G.GameID = M.GameID 
group by G.GameID

<强>输出:

 GameID    GameName    GameCover     ModName

   1        Benten       g1           mod1,mod2,mod3