简体   繁体   中英

Display selected results only from the database table

I would like to display only a selected result on my php page.

This is my php code:

$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>";
}

Actually this code above is displaying the whole list of records actually. I only want it to display selected record which i clicked on my first php page.

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>";
}

How did you get the values frmo the first page? GET or POST?

when you have the value(s), you can add an where clause to your SELECT Statement

$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"] . "'";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM