无法显示其他表格

时间:2017-02-14 16:32:20

标签: php sql

我对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>

2 个答案:

答案 0 :(得分: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个表。但正如你所说,你刚刚开始,我认为这个解决方案会做到。

答案 1 :(得分: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