仅显示数据库表中的选定结果

时间:2014-12-22 14:04:10

标签: php mysql mysqli

我想在我的php页面上只显示一个选定的结果。

这是我的PHP代码:

$bookdetsql = "
SELECT b.bookISBN
     , b.bookTitle
     , b.bookYear
     , b.catID
     , b.pubID
     , p.pubName
     , p.location
     , c.catDesc
     , b.bookPrice 
  FROM nbc_book b
  LEFT 
  JOIN nbc_category c 
    ON b.catID = c.catID 
  LEFT 
  JOIN nbc_publisher p 
    ON b.pubID = p.pubID
";


$bookdetrs = mysqli_query($conn, $bookdetsql) or die(mysqli_error($conn));

$bookdetnum = mysqli_num_rows($bookdetrs);

if($bookdetnum >= 1 ){
echo "<div style='margin: 0 0 10px 0; font-weight: bold;'>$bookdetnum record(s) found!</div>";
while ($row = mysqli_fetch_assoc($bookdetrs)) {
    echo "<tr>";
    echo "<td><center>" . $row['bookTitle']."</a></center></td>";
    echo "<td><center>" . $row['bookYear']."</center></td>";
    echo "<td><center>" . $row['catDesc']."</center></td>";
    echo "<td><center>" . $row['bookPrice']."</center></td>";
    echo "<td><center>" . $row['pubName']."</center></td>";
    echo "<td><center>" . $row['location']."</center></td>";
    echo "</tr>";
}

} else {
   echo "<b>Books not found!</b>";
}

实际上,上面这段代码实际上显示了整个记录列表。我只希望它显示我在第一个php页面上点击的所选记录。

2 个答案:

答案 0 :(得分:1)

Pleas Replace left join to join because left join is fetch matching and unmatch both record but when you use simple join it is work on inner join and fetch only match record according to your requirement   


$bookdetsql = "SELECT bookISBN, bookTitle, bookYear, nbc_book.catID, nbc_book.pubID, pubName,  location, catDesc, bookPrice FROM nbc_book JOIN nbc_category ON nbc_book.catID = nbc_category.catID JOIN nbc_publisher ON nbc_book.pubID = nbc_publisher.pubID";

$bookdetrs = mysqli_query($conn, $bookdetsql) or die(mysqli_error($conn));

$bookdetnum = mysqli_num_rows($bookdetrs);

if($bookdetnum >= 1 ){
echo "<div style='margin: 0 0 10px 0; font-weight: bold;'>$bookdetnum record(s) found!</div>";
while ($row = mysqli_fetch_assoc($bookdetrs)) {
    echo "<tr>";
    echo "<td><center>" . $row['bookTitle']."</a></center></td>";
    echo "<td><center>" . $row['bookYear']."</center></td>";
    echo "<td><center>" . $row['catDesc']."</center></td>";
    echo "<td><center>" . $row['bookPrice']."</center></td>";
    echo "<td><center>" . $row['pubName']."</center></td>";
    echo "<td><center>" . $row['location']."</center></td>";
    echo "</tr>";
}

} else {
   echo "<b>Books not found!</b>";
}

答案 1 :(得分:1)

你是如何从第一页获得价值的? GET还是POST?

当你有值时,可以在SELECT语句中添加where子句

$bookdetsql = "SELECT bookISBN, bookTitle, bookYear, nbc_book.catID, nbc_book.pubID, pubName,  location, catDesc, bookPrice 
                 FROM nbc_book 
            LEFT JOIN nbc_category ON nbc_book.catID = nbc_category.catID 
            LEFT JOIN nbc_publisher ON nbc_book.pubID = nbc_publisher.pubID
                WHERE bookISBN = '" . $_GET["bookISBN"] . "'";