PHP Outputting data from 2 tables with 3 joins

Dean :

hope you can help,

I have 3 tables
article
article_keywords_link
keywords

They are joined together, and im wanting to achieve 1 article to display on the page, but show all related keywords which is linked to the article.

I have not yet formatted this page, im just trying to get the functionality to work.

SELECT article.article_id, article.article_title, article.article_content, keywords.keyword_name
FROM article
JOIN article_keyword_link ON article.article_id = article_keyword_link.article_id
INNER JOIN keywords ON article_keyword_link.keyword_id = keywords.keyword_id

Running the above query in Phpmyadmin, i recieve these results

Query Output from PhpmyAdmin
+------------+---------------+-------------------+--------------+
| article_id | article_title |  article_content  | keyword_name |
+------------+---------------+-------------------+--------------+
|          1 | Article 1     | Article 1 content | CCU          |
|          1 | Article 1     | Article 1 content | WIFI         |
|          1 | Article 1     | Article 1 content | Network      |
|          2 | Article 2     | Article 2 content | Passenger    |
|          2 | Article 2     | Article 2 content | Application  |
+------------+---------------+-------------------+--------------+

Here is how the tables are populated

Table: article
+------------+---------------+-------------------+
| article_id | article_title |  article_content  |
+------------+---------------+-------------------+
|          1 | Article 1     | Article 1 content |
|          2 | Article 2     | Article 2 content |
+------------+---------------+-------------------+
Table: article_keywords_link
+------------+------------+
| article_id | keyword_id |
+------------+------------+
|          1 |          1 |
|          1 |          2 |
|          2 |          3 |
|          2 |          4 |
|          1 |          7 |
+------------+------------+
Table: keywords
+------------+--------------+
| keyword_id | keyword_name |
+------------+--------------+
|          1 | CCU          |
|          2 | WIFI         |
|          3 | Passenger    |
|          4 | Application  |
|          5 | Software     |
|          6 | Hardware     |
|          7 | Network      |
+------------+--------------+

Page source:

<?php

include 'includes/db.php';

// Create connection
$conn = new mysqli($host, $username, $password, $database);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$sql = "

SELECT article.article_id, article.article_title, article.article_content, keywords.keyword_name
FROM article
JOIN article_keyword_link ON article.article_id = article_keyword_link.article_id
INNER JOIN keywords ON article_keyword_link.keyword_id = keywords.keyword_id

";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "Article ID: " . $row["article_id"] . "<br>";
        echo "Article Title: " . $row["article_title"] . "<br>";
        echo "Article Content: " . $row["article_content"] . "<br>"
;
    }
} else {
    echo "0 results";
}
$conn->close();
?>

Page output:

Article ID: 1
Article Title: Article 1
Article Content: Article 1 content
Article ID: 1
Article Title: Article 1
Article Content: Article 1 content
Article ID: 1
Article Title: Article 1
Article Content: Article 1 content
Article ID: 2
Article Title: Article 2
Article Content: Article 2 content
Article ID: 2
Article Title: Article 2
Article Content: Article 2 content

So there's two problems im facing.
1. How can I retrieve just one article to display on the page, whilst showing all keywords relating to that article?
2. How can I adjust my source code to show the keywords? if i add a new echo in the loop for keyword_name, the page doesn't load.

Any help with this would be greatly appreciated

MHewison :

You're almost there, try this.

SELECT article.article_id, article.article_title, article.article_content, group_concat(keywords.keyword_name) as keywords
FROM article
JOIN article_keyword_link ON article.article_id = article_keyword_link.article_id
INNER JOIN keywords ON article_keyword_link.keyword_id = keywords.keyword_id
GROUP BY article.article_id

for information on the functions used to acheive this. please see.

https://dev.mysql.com/doc/refman/8.0/en/group-by-functions.html

Additionally, if you just want one article you add limit 1 to the end of your query. and if you want the newest/oldest you would add ORDER BY article_id DESC LIMIT 1 for newest and ORDER BY article_id ASC LIMIT 1 for newest

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=405068&siteId=1