K
K
kafkiansky2018-06-25 19:35:12
PHP
kafkiansky, 2018-06-25 19:35:12

How to display a comment below the article under which it was written using PDO PHP?

Hello.
I have two tables:

articles(id, title, slug, content, author_id, date)
and comments(id, author_id, article_id, content, date). I want to display the comments below the post it's set under, i.e. compare if the id from the articles table matches the article_id from the comments table. This is a self-written blog, no templates or patterns, I use PDO PHP. I roughly do the following:
$stmt = $db->query('SELECT * FROM articles');   
$comments = $db->prepare('SELECT * FROM comments WHERE article_id = ?');

It's not entirely clear what to do next. How to proceed?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
Y
Yan-s, 2018-06-25
@Yan-s

This may be terrible code, but I've only been familiar with this for a month, sorry.

Yes, this is terrible code. Especially for a month of study, for which the documentation could be read 5 times in its entirety. Instead: php.net/manual/en/reserved.variables.get.php php.net/manual/en/language.variables.external.php
It is highly discouraged to interfere with HTML markup with logic that is not related to it. Especially with the logic of working with the database. The markup must be in a separate file.
Good practices for beginners:
getjump.github.io/en-php-the-right-way

F
FanatPHP, 2018-06-26
@FanatPHP

$stmt = $db->prepare('SELECT * FROM comments WHERE article_id = ?');
$stmt->execute([$_GET['post_id']]);
$comments = $stmt->fetchAll(PDO::FETCH_OBJ)

And then you already output from the $comments array in the markup. The simplest example: phpfaq.ru/tpl

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question