选择时
count(*)
$number_of_rows = $result->fetchColumn(); // This contains 1 row, with the column indicating the count and so your condition will never be executed
而是将列命名为
SELECT count(*) as total_posts FROM posts
$row = $stmt->fetch(PDO::FETCH_ASSOC); // Fetch the first row
$total_posts = $row['total_posts']; // Get the column containing the result of the count
// Run your condition $total_posts
Checkout this answer