发现很难获得PHP中每个博客文章的评论数

时间:2019-01-26 23:42:04

标签: php mysql sql

我正在使用PHP和MYSQL建立一个网站,并且在如何获取每个帖子的评论数上陷入困境。我尝试做一些事情,但这给了我所有帖子的已批准评论总数。而不是下面的屏幕快照所示的个人帖子。

Screenshot of the frontend

这是我尝试过的。

我在INDEX.PHP上的代码

$get_comment = "SELECT * FROM comments WHERE status = 'approve'";
$get_comment_run = mysqli_query($con, $get_comment);
$num_of_rows = mysqli_num_rows($get_comment_run);

if($num_of_rows > 0) {
    echo "<span><i class='fas fa-comments'></i> $num_of_rows</span>";
}
else {
    echo "0";
}

这是我的表结构或注释表的创建表脚本。

 -- phpMyAdmin SQL Dump
-- version 4.8.0.1
-- https://www.phpmyadmin.net/
--
-- Host: 127.0.0.1
-- Generation Time: Jan 28, 2019 at 12:15 AM
-- Server version: 10.1.32-MariaDB
-- PHP Version: 7.2.5

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0;
START TRANSACTION;
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;

--
-- Database: `cms`
--

-- --------------------------------------------------------

--
-- Table structure for table `comments`
--

CREATE TABLE `comments` (
  `id` int(11) NOT NULL,
  `date` int(11) NOT NULL,
  `name` varchar(255) NOT NULL,
  `username` varchar(255) NOT NULL,
  `post_id` int(11) NOT NULL,
  `email` varchar(255) NOT NULL,
  `website` varchar(255) NOT NULL,
  `image` varchar(255) NOT NULL,
  `comment` text NOT NULL,
  `status` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Dumping data for table `comments`
--

INSERT INTO `comments` (`id`, `date`, `name`, `username`, `post_id`, `email`, `website`, `image`, `comment`, `status`) VALUES
(1, 1548612873, 'Val Amasiatu', 'compus', 47, 'valamasiatu@gmail.com', 'avidscholars.com.ng', 'Unknown_Member.jpg', 'Cool', 'approve');

--
-- Indexes for dumped tables
--

--
-- Indexes for table `comments`
--
ALTER TABLE `comments`
  ADD PRIMARY KEY (`id`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `comments`
--
ALTER TABLE `comments`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=2;
COMMIT;

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

和张贴表格的文字

-- phpMyAdmin SQL Dump
-- version 4.8.0.1
-- https://www.phpmyadmin.net/
--
-- Host: 127.0.0.1
-- Generation Time: Jan 28, 2019 at 12:19 AM
-- Server version: 10.1.32-MariaDB
-- PHP Version: 7.2.5

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0;
START TRANSACTION;
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;

--
-- Database: `cms`
--

-- --------------------------------------------------------

--
-- Table structure for table `posts`
--

CREATE TABLE `posts` (
  `id` int(11) NOT NULL,
  `date` int(11) NOT NULL,
  `title` varchar(255) NOT NULL,
  `author` varchar(255) NOT NULL,
  `author_image` varchar(255) NOT NULL,
  `image` varchar(255) NOT NULL,
  `categories` varchar(255) NOT NULL,
  `tags` varchar(255) NOT NULL,
  `post_data` text NOT NULL,
  `views` int(11) NOT NULL,
  `status` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Dumping data for table `posts`
--

INSERT INTO `posts` (`id`, `date`, `title`, `author`, `author_image`, `image`, `categories`, `tags`, `post_data`, `views`, `status`) VALUES
(42, 1548346810, 'What is Lorem Ipsum?', '1234', 'AvePoint.jpg', 'administration-balance-blur-272978.jpg', 'tutorials', 'my, my, my', '<p>Accidentally deleted a closing div tag. And have tried all night but couldn\'t locate it. I have also tried some online tools but without success. I have counted with notepad++ and found 22 and 21 , meaning that one is missing. But what i have not been able to do is to find and place this missing closing tag div where it is supposed to be.</p>\r\n<p>Accidentally deleted a closing div tag. And have tried all night but couldn\'t locate it. I have also tried some online tools but without success. I have counted with notepad++ and found 22 and 21 , meaning that one is missing. But what i have not been able to do is to find and place this missing closing tag div where it is supposed to be.</p>\r\n<p>Accidentally deleted a closing div tag. And have tried all night but couldn\'t locate it. I have also tried some online tools but without success. I have counted with notepad++ and found 22 and 21 , meaning that one is missing. But what i have not been able to do is to find and place this missing closing tag div where it is supposed to be.</p>', 4, 'publish'),
(43, 1548346917, 'Another test post', '1234', 'AvePoint.jpg', '33754-bible1200-800w-tn.jpg', 'books', 'Testing and observing', '<h2>Before you start</h2>\r\n<p><strong>Make sure&nbsp;<a href=\"http://validator.w3.org/\">your code is valid</a></strong>, or you could get unexpected results. The script assumes you have valid HTML5 code, but would like to make sure you didn\'t leave any tags, unintentionally, unclosed.</p>\r\n<p>This script doesn\'t check for self closing tags.</p>\r\n<p>&nbsp;</p>', 3, 'publish'),
(44, 1548352991, 'A unique post', '1234', 'AvePoint.jpg', 'prayer.jpg', 'Themes', 'Testing and useful', '<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Maxime, fuga mollitia ducimus error nobis numquam perferendis vitae quam obcaecati similique optio non beatae in voluptas dolores, praesentium, architecto nisi ab. Lorem ipsum dolor sit amet, consectetur adipisicing elit. Maxime, fuga mollitia ducimus error nobis numquam perferendis vitae quam obcaecati similique optio non beatae in voluptas dolores, praesentium, architecto nisi ab.</p>\r\n<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Maxime, fuga mollitia ducimus error nobis numquam perferendis vitae quam obcaecati similique optio non beatae in voluptas dolores, praesentium, architecto nisi ab. Lorem ipsum dolor sit amet, consectetur adipisicing elit. Maxime, fuga mollitia ducimus error nobis numquam perferendis vitae quam obcaecati similique optio non beatae in voluptas dolores, praesentium, architecto nisi ab.</p>', 4, 'publish'),
(46, 1548353597, 'Guidelines', '1234', 'AvePoint.jpg', 'dove.jpg', 'pro-tips', 'Spirit', '<h2>Before you start</h2>\r\n<p><strong>Make sure&nbsp;<a href=\"http://validator.w3.org/\">your code is valid</a></strong>, or you could get unexpected results. The script assumes you have valid HTML5 code, but would like to make sure you didn\'t leave any tags, unintentionally, unclosed.</p>\r\n<p>This script doesn\'t check for self closing tags.</p>', 17, 'publish'),
(47, 1548353755, 'Stackoverflow Question', '1234', 'AvePoint.jpg', 'Prayer-Man.jpg', 'pro-tips', 'going, praying', '<p>Accidentally deleted a closing div tag. And have tried all night but couldn\'t locate it. I have also tried some online tools but without success. I have counted with notepad++ and found 22 and 21 , meaning that one is missing. But what i have not been able to do is to find and place this missing closing tag div where it is supposed to be.</p>\r\n<p>Accidentally deleted a closing div tag. And have tried all night but couldn\'t locate it. I have also tried some online tools but without success. I have counted with notepad++ and found 22 and 21 , meaning that one is missing. But what i have not been able to do is to find and place this missing closing tag div where it is supposed to be.</p>', 9, 'publish'),
(48, 1548614342, 'What is Lorem Ipsum?', 'val', 'adult-beard-blur-927022.jpg', 'hero.jpg', 'money', 'come', '<p>The new editor will be enabled by default in the next major release of WordPress. If you&rsquo;re not sure how compatible your current themes and plugins&nbsp; The new editor will be enabled by default in the next major release of WordPress. If you&rsquo;re not sure how compatible your current themes and plugins&nbsp; The new editor will be enabled by default in the next major release of WordPress. If you&rsquo;re not sure how compatible your current themes and plugins&nbsp;The new editor will be enabled by default in the next major release of WordPress. If you&rsquo;re not sure how compatible your current themes and plugins.</p>\r\n<p>The new editor will be enabled by default in the next major release of WordPress. If you&rsquo;re not sure how compatible your current themes and plugins&nbsp;themes and plugins&nbsp;The new editor will be enabled by default in the next major release of WordPress. If you&rsquo;re not sure how compatible your current themes and plugins.</p>', 2, 'publish');

--
-- Indexes for dumped tables
--

--
-- Indexes for table `posts`
--
ALTER TABLE `posts`
  ADD PRIMARY KEY (`id`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `posts`
--
ALTER TABLE `posts`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=49;
COMMIT;

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

2 个答案:

答案 0 :(得分:-1)

您还需要将每个博客文章的唯一ID添加到数据库表中(如果尚未添加),然后在添加评论时,将评论插入数据库中时,将博客文章ID分配给该评论,然后在搜索评论时执行:

$get_comment = "SELECT * FROM comments WHERE post_id = $post_id && status = 'approve'";

$ post_id将是博客文章的唯一ID。

post_id必须存在于您的数据库中。

答案 1 :(得分:-1)

您的comment表中缺少约束,字段post_id应该是对特定FOREIGN KEY的引用(posts)。

我强烈建议您更改表注释并添加此约束,这将增强完整性并帮助执行查询,因为这些字段将引用索引:

ALTER TABLE `comments`
ADD FOREIGN KEY (`post_id`)
REFERENCES `posts` (`id`);
-- Make sure to read the part below before adding this constraint to your table.

这样做将阻止您删除包含评论的帖子,您必须先删除评论,或者可以添加操作来执行:

ALTER TABLE `comments`
ADD FOREIGN KEY (`post_id`)
REFERENCES `posts` (`id`)
ON DELETE CASCADE; -- This will delete the comment if a post is deleted

要获取每个帖子的评论数,您必须使用COUNT语句和GROUP BY组合:

SELECT COUNT(`id`) AS "CommentCount",
       `post_id`
FROM `comments`
WHERE `status` = "approve"
GROUP BY `post_id`;

然后,在您的php代码中:

$get_comment_run = mysqli_query($con, $get_comment); //$get_comment being, of course, the above query

while ($row = mysqli_fetch_array($get_comment_run, MYSQLI_ASSOC))
{
    if($row["CommentCount"] > 0)
    {
        echo "<span><i class='fas fa-comments'></i>There is " . $row["CommentCount"] . " comments for the post id " . $row["post_id"] . "</span>";
    }
    else
    {
        echo "0";
    }
}

表中具有简化数据的示例:

模式(MySQL v5.7)

CREATE TABLE comments
(
  id INT PRIMARY KEY AUTO_INCREMENT NOT NULL,
  text VARCHAR(255),
  `status` VARCHAR(255),
  post_id INT NOT NULL
);

INSERT INTO comments VALUES (default, "a", "approve", 1),
                            (default, "b", "approve", 1),
                            (default, "c", "approve", 1),
                            (default, "d", "pending", 1),
                            (default, "e", "approve", 1),
                            (default, "f", "rejected", 1), -- 4 approved for post_id 1
                            (default, "a", "approve", 2),
                            (default, "b", "approve", 2),
                            (default, "c", "approve", 2),
                            (default, "d", "pending", 2),
                            (default, "e", "approve", 2), -- 4 approved for post_id 2
                            (default, "f", "rejected", 3),
                            (default, "a", "approve", 3),
                            (default, "b", "approve", 3), -- 2 approved for post_id 3
                            (default, "c", "approve", 4),
                            (default, "d", "pending", 4), -- 1 approved for post_id 4
                            (default, "e", "approve", 5), -- 1 approved for post_id 5
                            (default, "f", "rejected", 6), -- 0 approved for post_id 6
                            (default, "a", "approve", 7),
                            (default, "b", "approve", 7),
                            (default, "c", "approve", 7), -- 3 approved for post_id 7
                            (default, "d", "pending", 8), -- 0 approved for post_id 8
                            (default, "e", "approve", 9), -- 1 approved for post_id 9
                            (default, "f", "rejected", 10);  -- 0 approved for post_id 10

查询#1

SELECT COUNT(`id`) AS "CommentCount",
       `post_id`
FROM `comments`
WHERE `status` = "approve"
GROUP BY `post_id`;

输出

| CommentCount | post_id |
| ------------ | ------- |
| 4            | 1       |
| 4            | 2       |
| 2            | 3       |
| 1            | 4       |
| 1            | 5       |
| 3            | 7       |
| 1            | 9       |

View on DB Fiddle

相关问题