php - Remove book from search results if it has already been purchased


I am trying to check the books_ordered table to get a list of book ISBN's that have been ordered. If an ISBN exists in teh books_ordered table I do not want it to show in the list of search results. I have the following code:

include "include.php";

$query1 = 'SELECT isbn as myisbn FROM books_ordered';
$result1 = mysqli_query($con, $query1) or die("Error in query $query1: " . mysqli_error());
while($row = mysqli_fetch_array($result1)){

$query = 'SELECT * FROM book';
$result = mysqli_query($con, $query) or die("Error in query $query: " . mysqli_error());

while ($row = mysqli_fetch_array($result)){
echo $row[1]."<br>";
mysqli_close($con);             //closes the connection


The script I have only hides the first isbn from the books_ordered table in the search results, does anyone have any ideas why? I'm guessing it's something to do with the brackets in the while loops, i just can't figure it out. Or if anyone knows a better way of doing this it would be much appreciated. Thanks.




It would be more efficient to filter out the unwanted results from the book table in a single query, like so:

select b.* from book b
left join books_ordered o on b.isbn = o.isbn
where o.isbn is null



You are only storing the last value in $isbn since it is not an array. You need to add the isbn into an array, and then check each row in the second query against that array.

But I would probably do the processing in the SQL statement instead. Something like:

SELECT * FROM book where isbn not in (select isbn from books_ordered)



What is your database structure for "book"? You can easily make a "JOIN"-Command.




If I have understood well, you want to show only the books are not ordered yet.

You could use this SQL sentence:

SELECT * FROM book WHERE isbn_row NOT IN (SELECT isbn FROM books_ordered)

First we select the isbn of the ordered books with:SELECT isbn FROM books_ordered When we have the list, using the clause NOT IN we can select from BOOK all books except all that we get the isbn in the first sentence.

I hope is useful for you.

People are also looking for solutions to the problem: php - How to sort a massive/object


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.