2017-02-14 95 views
1

我對SQL和PHP有點新瞭如何顯示帶有三個不同表的輸出?我只能在一個表格中顯示它,但我不知道如何將它們分組在3個不同的表格中?下面是輸出:無法顯示其他表

enter image description here

<?php 

include "Connection.php"; 

$sql = "SELECT artist.artistName, artistcd.artistID, artistcd.cdID, artistcd.cdTitle, artistcd.cdGenre, artistcd.cdPrice FROM artist, artistcd WHERE artist.artistID = artistcd.artistID ORDER BY artistcd.artistID" ; 


$query = mysqli_query($connect, $sql) or die("Error: ".mysqli_error($connect));; 


?> 
<table width="70%" cellpadding="5" cellspace="5"> 

<tr> 
    <th>Genre</th> 
    <th>CD Identification</th> 
    <th>Title</th> 
    <th>Price</th> 
    </tr> 

<?php 

    $name = mysqli_fetch_assoc($query); 
    echo $name["artistID"] . $name["artistName"]; 

    while ($row = mysqli_fetch_assoc($query)) { 
     echo "<tr>"; 
     echo "<td>" . $row['cdGenre']. "</td>"; 
     echo "<td>" . $row['cdID']. "</td>"; 
     echo "<td>" . $row['cdTitle']. "</td>"; 
     echo "<td>" . $row['cdPrice']. "</td>"; 
     echo "</tr>"; 
} 
?> 

<table width="70%" cellpadding="5" cellspace="5"> 

</table> 
+0

花空數組,然後同時申請一次,並指定所有的值該數組,然後使用該數組變量分爲三個不同的表 –

+0

有什麼區別3桌之間?有沒有需要做的分組?您可能需要進行3個獨立的查詢。或者在for循環中定義一個用於在3分割結果的邏輯。 –

+0

@LeonelAtencio是的3個表顯示3個不同的藝術家的名字,並有3個不同的歌曲 –

回答

0

好,最簡單的事情是爲你重複代碼塊3倍。每一次,與傳遞給查詢不同的藝術家ID:

<?php 
include "Connection.php"; 
$sql = "SELECT artist.artistName, artistcd.artistID, artistcd.cdID, artistcd.cdTitle, artistcd.cdGenre, artistcd.cdPrice 
     FROM artist, artistcd 
     WHERE artist.artistID = "$ArtistID1"; 

$query = mysqli_query($connect, $sql) or die("Error: ".mysqli_error($connect));; 
$name = mysqli_fetch_assoc($query); 
    echo "<p>" . $name["artistID"] . $name["artistName"] . "</p>"; 
?> 

<table width="70%" cellpadding="5" cellspace="5"> 
<tr> 
    <th>Genre</th> 
    <th>CD Identification</th> 
    <th>Title</th> 
    <th>Price</th> 
    </tr> 

    <?php 
     while ($row = mysqli_fetch_assoc($query)) { 
      echo "<tr>"; 
      echo "<td>" . $row['cdGenre']. "</td>"; 
      echo "<td>" . $row['cdID']. "</td>"; 
      echo "<td>" . $row['cdTitle']. "</td>"; 
      echo "<td>" . $row['cdPrice']. "</td>"; 
      echo "</tr>"; 
    } 
    ?> 

<table width="70%" cellpadding="5" cellspace="5"> 
</table> 


<?php 
$sql = "SELECT artist.artistName, artistcd.artistID, artistcd.cdID, artistcd.cdTitle, artistcd.cdGenre, artistcd.cdPrice 
     FROM artist, artistcd 
     WHERE artist.artistID = "$ArtistID2"; 

$query = mysqli_query($connect, $sql) or die("Error: ".mysqli_error($connect));; 
$name = mysqli_fetch_assoc($query); 
    echo "<p>" . $name["artistID"] . $name["artistName"] . "</p>"; 
?> 

<table width="70%" cellpadding="5" cellspace="5"> 
<tr> 
    <th>Genre</th> 
    <th>CD Identification</th> 
    <th>Title</th> 
    <th>Price</th> 
    </tr> 

    <?php 
     while ($row = mysqli_fetch_assoc($query)) { 
      echo "<tr>"; 
      echo "<td>" . $row['cdGenre']. "</td>"; 
      echo "<td>" . $row['cdID']. "</td>"; 
      echo "<td>" . $row['cdTitle']. "</td>"; 
      echo "<td>" . $row['cdPrice']. "</td>"; 
      echo "</tr>"; 
    } 
    ?> 

<table width="70%" cellpadding="5" cellspace="5"> 
</table> 


<?php 
$sql = "SELECT artist.artistName, artistcd.artistID, artistcd.cdID, artistcd.cdTitle, artistcd.cdGenre, artistcd.cdPrice 
     FROM artist, artistcd 
     WHERE artist.artistID = "$ArtistID3"; 

$query = mysqli_query($connect, $sql) or die("Error: ".mysqli_error($connect));; 
$name = mysqli_fetch_assoc($query); 
    echo "<p>" . $name["artistID"] . $name["artistName"] . "</p>"; 
?> 

<table width="70%" cellpadding="5" cellspace="5"> 
<tr> 
    <th>Genre</th> 
    <th>CD Identification</th> 
    <th>Title</th> 
    <th>Price</th> 
    </tr> 

    <?php 
     while ($row = mysqli_fetch_assoc($query)) { 
      echo "<tr>"; 
      echo "<td>" . $row['cdGenre']. "</td>"; 
      echo "<td>" . $row['cdID']. "</td>"; 
      echo "<td>" . $row['cdTitle']. "</td>"; 
      echo "<td>" . $row['cdPrice']. "</td>"; 
      echo "</tr>"; 
    } 
    ?> 

<table width="70%" cellpadding="5" cellspace="5"> 
</table> 

另一種方法是爲所有的藝術家查詢一次,並通過直接從關聯數組過濾藝術家打造的3個表。但正如你所說,你剛剛開始,我認爲這個解決方案會做到這一點。

+0

解析錯誤:語法錯誤,意外'$ ArtistID1'(T_VARIABLE)在C:\ xampp \ htdocs \ Exer42 \新建文件夾\ display_music.php on line 15 –

+0

您需要替換$ ArtistID1,$ ArtistID2和$ ArtistID3與實際的藝術家ID!... –

0

如果該人員的身份證號在所有3張桌子上,您可以做什麼。

您可以使用JOIN

See This For More Info

SELECT mytable1.id, mytable2.order, mytable3.price FROM mytable1 WHERE mytable1.id=7 JOIN mytable2 ON mytable1.id=mytable2.id JOIN mytable3 ON mytable1.id= mytable3.id