ayumou
ayumou

Reputation: 13

PHP primary key table from foreign key

Table 1: books (book_id as primary key)

book_id|title
1      |apple
2      |banana
3      |volkswagen

table 2: genre (genre_id as primary key)

genre_id|name
1       |fruits
2       |cars

Table 3:multiple (id as primary book_id and genre_id as foreign key) //to put more genre in 1 book

id|book_id|genre_id
1 |   1   |  1
2 |   3   |  2
3 |   2   |  1

I wanted to show all books with genre as fruits on alphabetical order my code is:

<?php
include "mysql-connect.php";
$sql = 
    "SELECT title 
    FROM books
    INNER JOIN multiple ON book.book_id = multiple.book_id
    VALUES ('1')";
if (mysqli_query($conn, $sql)) {
    while($row = $result->fetch_assoc()) {
        echo "" . $row["title"]."<br>";
    }
} else {
    echo "0 results";
}
$conn->close();
?>

Still showing 0 results after changing the values

Upvotes: 0

Views: 33

Answers (1)

ArtisticPhoenix
ArtisticPhoenix

Reputation: 21671

Whats this?

SELECT title 
FROM books
   INNER JOIN multiple 
   ON book.book_id = multiple.book_id VALUES ('1')

Doesn't look like a valid SELECT query to me, where is the WHERE. Typical Values is uses in insert

  INSERT INTO multiple (id) VALUES (1)

Probably you need something like this

SELECT title 
FROM books
   INNER JOIN multiple 
   ON book.book_id = multiple.book_id
WHERE
   some_column = 1

What this is some_column I have no idea.

Upvotes: 0

Related Questions