PHP-博客帖子评论系统
我在向博客帖子系统添加评论时遇到问题,我使用OOP PHP完成了此操作,下面是我的代码:PHP-博客帖子评论系统,php,oop,mysqli,Php,Oop,Mysqli,我在向博客帖子系统添加评论时遇到问题,我使用OOP PHP完成了此操作,下面是我的代码: public function getBlogPosts() { $stmt = $this->mysqli->prepare("SELECT id, posted_by, date_time, title, message FROM blog_posts"); $stmt->execute(); $stmt->bind_result($id, $posted
public function getBlogPosts() {
$stmt = $this->mysqli->prepare("SELECT id, posted_by, date_time, title, message FROM blog_posts");
$stmt->execute();
$stmt->bind_result($id, $posted_by, $date_time, $title, $message);
$stmt->store_result();
$stmt2 = $this->mysqli->prepare("SELECT id, posted_by, date_time, post_id, message FROM blog_comments");
$stmt2->execute();
$stmt2->bind_result($c_id, $c_posted_by, $c_date_time, $c_post_id, $c_message);
$stmt2->store_result();
while($stmt->fetch() && $stmt2->fetch()) {
echo "<h3>$title</h3>Posted by: $posted_by at $date_time.<br /><br />$message<br />";
if($stmt2->num_rows > 0) {
$stmt3 = $this->mysqli->prepare("SELECT post_id FROM blog_comments WHERE post_id=?");
$stmt3->bind_param('s', $id);
$stmt3->execute();
$stmt3->bind_result($n_id);
$stmt3->store_result();
if($stmt3->num_rows > 0) {
while($stmt3->fetch() && $stmt2->fetch()) {
echo "<br /><br />" . $c_posted_by . " at " . $c_date_time . "<br />" . $c_message;
}
}
$stmt3->close();
}
}
$stmt->close();
$stmt2->close();
}
公共函数getBlogPosts(){
$stmt=$this->mysqli->prepare(“选择id、发布者、日期、时间、标题、来自博客的消息”);
$stmt->execute();
$stmt->bind_result($id、$posted_by、$date_time、$title、$message);
$stmt->store_result();
$stmt2=$this->mysqli->prepare(“选择id、发布者、日期和时间、发布id、来自博客的消息和评论”);
$stmt2->execute();
$stmt2->bind_result($c_id,$c_posted_by,$c_date_time,$c_post_id,$c_message);
$stmt2->store_result();
而($stmt->fetch()&&$stmt2->fetch()){
echo“$titlePosted by:$date\u time时的发布者。$message
”; 如果($stmt2->num_rows>0){ $stmt3=$this->mysqli->prepare(“从博客评论中选择post\u id,其中post\u id=?”; $stmt3->bind_参数('s',$id); $stmt3->execute(); $stmt3->bind_result($n_id); $stmt3->store_result(); 如果($stmt3->num_rows>0){ 而($stmt3->fetch()&&$stmt2->fetch()){ 回显“
”在“$c_日期\时间”发布的$c_消息; } } $stmt3->close(); } } $stmt->close(); $stmt2->close(); }
在该代码中,我试图获取所有博客文章,如果表'post_id'与博客文章记录的id匹配,则获取博客文章的博客评论。如果您能发现错误,我将非常高兴,这将帮助许多试图修复此类错误的人。谢谢大家! 我已经对它进行了全面的重新编码,效果很好。我没有给出代码,但逻辑是正确的 //如果有博客文章,则回显博客文章并存储id //存储id后,使用它检查是否有任何与该id相关的博客评论,并尝试链接它们
//如果有一个id链接到blog_评论,则将其回显。易于理解的如果您有问题,请告诉我。您有任何错误或警告吗?没有,我没有收到任何错误,但我已经收到了。我的逻辑是错误的。我已经对它进行了完全的重新编码,它工作得很好。我没有给出代码,但逻辑是//如果有博客文章,则回显博客文章并存储id//存储id后,使用它检查是否有任何与该id相关的博客注释并尝试链接它们//如果有一个id链接到blog_评论,则将其回显。易于理解的如果你有问题,请告诉我。