Answer the question
In order to leave comments, you need to log in
How to correctly build a query structure for selecting from several tables?
Good day. The essence of the question is this: there is a table with articles, there is a table with categories. I want to join two tables with a single query, while doing this (screenshot). Am I on the right track? + There will also be a table with users who are also linked to articles with keys. How can I query the database in this case? I'm thinking of joining everything in one query with inner joins. Please tell me how it is done correctly. Thank you.
//функция выборки одной статьи по id
function out_article_id($id){
global $con;
$query = "SELECT c.name AS cat, a.* FROM category c INNER JOIN articles a ON c.id = a.category WHERE a.id = $id;";
$query_result = mysqli_query($con, $query);
$query_result_top = mysqli_fetch_all($query_result, MYSQLI_ASSOC);
if ($query_result_top) {
return $query_result_top;
}else {
return false;
}
}
//функция выборки всех статей
function out_category(){
global $con;
$query = "SELECT c.name AS cat, a.* FROM category c INNER JOIN articles a ON c.id = a.category;";
$query_result = mysqli_query($con, $query);
$query_result_top = mysqli_fetch_all($query_result, MYSQLI_ASSOC);
if ($query_result_top) {
return $query_result_top;
}else {
return false;
}
}
<?php foreach ($article as $value): ?>
<div class="article-item">
<div class="article-item-title"><?=$value['title'];?></div>
<div class="article-item-text"><?=$value['text'];?></div>
<div class="article-property-holder">
<div class="article-item-date">Дата публикации: <?=$value['date'];?></div>
<div class="article-item-aythor">Автор: <?=$value['author'];?></div>
<div class="article-item-category">Категория: <?=$value['cat'];?></div>
</div>
</div>
<?php endforeach; ?>
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question