2017-02-14 92 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