I want to list comments from my database depending on their type.
There are three types of comments in my database and I call them with three different queries.
//01 - Awaiting Comments
$query = $handler->prepare("SELECT * FROM comments WHERE confirmed = 0");
$query->execute();
$r = $query->fetchAll(PDO::FETCH_ASSOC);
echo "<h1>Awaiting Comments</h1>";
foreach($r as $r_) {
echo "<li>r_[title]</li>";
}
//02 - Comments waiting for confirmation
$query = $handler->prepare("SELECT * FROM comments WHERE confirmed = 2");
$query->execute();
$r = $query->fetchAll(PDO::FETCH_ASSOC);
echo "<h1>Comments waiting for confirmation</h1>";
foreach($r as $r_) {
echo "<li>r_[title]</li>";
}
//03 - Confirmed comments
$query = $handler->prepare("SELECT * FROM comments WHERE confirmed = 1");
$query->execute();
$r = $query->fetchAll(PDO::FETCH_ASSOC);
echo "<h1>Confirmed Comments</h1>";
foreach($r as $r_) {
echo "<li>r_[title]</li>";
}
With my current code i get the output i want like that:
Awaiting Comments
-comment 1
-comment 8
-comment 5
Comments waiting confirmation
-comment 9
-comment 4
-comment 2
Confirmed Comments
-comment 3
-comment 6
-comment 7
Is there any way to get same output with single query instead of three of them?
SELECT count(*) FROM comments WHERE confirmed in (0,1,2) GROUP BY confirmed
?