2015-07-21 61 views
2

我有2張桌子,書和用戶。兩個表都有ID將它們連接在一起。我想顯示用戶添加的書籍的圖像。我做了一個查詢來匹配相關的ID,這是輸出。如何通過相同的ID顯示多個記錄?

id | user_name | image 
------------------------------------ 
6 | d   | image.png 
------------------------------------ 
3 | bb   | tfos.jpg 
------------------------------------ 
6 | d   | the-business-plan.jpg 
------------------------------------ 
3 | bb   | front-page.jpg 
------------------------------------ 

現在的問題是,當我登錄的用戶「d」僅被顯示是「image.png」而如圖「d」具有2只記錄一個圖像。

這些都是我把作爲現在相關的代碼:

$query3 = "SELECT * FROM user, book WHERE user_name='$entered_username' AND book.id = user.id "; 
$result3 = mysqli_query($link, $query3) or die(mysqli_error($link)); 
if (mysqli_num_rows($result3) != 0) { 
$rowz = mysqli_fetch_array($result3); 
} else { //record not found 
echo "No record found"; 
} 

在HTML中顯示:

 <div class="panel panel-default"><div class="panel-heading"><i>Books</i> youadd</div><div class="panel-body"> <img width="150"src="images/<?php echo $rowz['image']; ?>"/></div></div> 

我該如何解決這個問題?

回答

0

使用循環來獲取結果

 
<?php 
while($rowz = mysqli_fetch_array($result3)) 
{ 
?> 
<!-- fetch result here, thus you get 2 results --> 
<div class="panel panel-default"><div class="panel-heading"><i>Books</i> youadd</div><div class="panel-body"> <img width="150"src="images/<?php echo $rowz['image']; ?>"/> 
<?php 
} 
?> 
相關問題