问:伙计们我试图使用以下查询方法将评论表连接到帖子表。
公共功能 feedView ($ session ,$ friend ,$ updateid ) {
$ sql2 = “选择u.update_ body,u.author,u.time,u.title,u.account_ name,u.update_ id,”
。 “ c.comment_ body,c.os_ id,c.author_ c,c.time_ c,c.comment_ id,c.type_ c”
。 “通过更新u加入注释_更新c”
。 “在c.os_ id =:statusid上,u.account_ name =:session或u.account_ name =:friend和(u.type ='a'或'c')的排序u.time asc,c.time_ c desc“ ;
$ stmth = $ this ->_ db-> prepare($ sql2);
$ stmth- > bindValue (“:session” , $ _ SESSION ['uname']);
$ stmth- > bindValue (“:friend” ,$ friend );
$ stmth- > bindValue (“:statusid” ,$ updateid );
$ stmth- > execute ();
返回 $ stmth- > fetchAll ( PDO :: FETCH _ ASSOC);
}
它可以很好地打印帖子,但是问题出在评论内容上,它似乎两次在同一帖子中打印评论。不知道错误来自哪里。
这是数据库模式:
这是显示提要的代码:
<?php
包括 “ includes / dbconfig.inc.php” ;
$ status &#95; replies =“”;
$ status &#95; list =“”;
$ statusui &#95; edit =“”;
$ isowner = “” ;
$ is &#95; friend =“”;
$ friends = array ();
$ stmt = $ conn- >准备(“从朋友中选择一个,其中一个,”
。 “((friend&#95; one =:session或friend&#95; two =:session)并被接受='1'” );
$ stmt- > bindparam (“:session” , $ &#95; SESSION ['uname']);
$ stmt- > execute ();
foreach ($ stmt- > fetchAll ( PDO :: FETCH &#95; ASSOC)as $ i => $ r){
$ r [ “ friend&#95; one” ] == $ &#95; SESSION ['uname']吗?$ friends [] = $ r [“ friend&#95; two”]:$ friends [] = $ r [“ friend&#95; one”];
$ friend = $ friends [ $ i ];
//从db中的用户表中获取update&#95; id并将其注入到feed查询中。
$ status2view = $项目- > statusView ( $ &#95; SESSION [ 'UNAME']);
foreach ($ status2view as $ val ) {
$ updateid = $ val [ 'update&#95; id' ];
//使用以下查询选择所有相关的帖子和评论并进行打印。
$ feedView = $项目- > feedView ( $ &#95; SESSION [ 'UNAME'],$朋友,$ updateid);
foreach ($ feedView 为 $ row1 ) {
$ status &#95; reply&#95; id = $ row1 ['comment&#95; id'];
$ reply &#95; d = htmlentities($ row1 ['comment&#95; body']);
$ reply &#95; data = stripslashes($ reply&#95; d);
$ reply &#95; osid = $ row1 ['os&#95; id'];
$ reply &#95; date = $ row1 ['time&#95; c'];
$ reply &#95; author = $ row1 ['author&#95; c'];
$ updateid = $ row1 [ 'update&#95; id' ];
$ account &#95; name = $ row1 ['account&#95; name'];
$ os &#95; id = $ row1 ['os&#95; id'];
$ author = $ row1 [ 'author' ];
$ post &#95; date = $ row1 ['time'];
$ title = 斜杠($ row1 [ 'title' ]);
$ data = 斜杠($ row1 [ 'update&#95; body' ]);
$ statusdeletebutton = '' ;
$ reply &#95; delete&#95; button =“”;
如果 ($ reply &#95; author == $&#95; SESSION ['uname']){
$ reply &#95; delete&#95; button =“ <li> <a href ='#'type ='”。$ status&#95; reply&#95; id。“'class ='delete&#95; reply&# 95; btn glyphicon glyphicon-trash delete&#95; reply&#95;“。$ status&#95; reply&#95; id。”'title ='删除此评论'>删除</a> </ span> </ li >“;
}
$ status &#95; replies =“ <div class ='replyboxes左上角答复&#95;”。$ status&#95; reply&#95; id。“'>”
。 “回复:-”
。 “ <a href ='home.php?u =” 。$ reply &#95; author。“'>”。$ reply&#95; author。“ </a>”
。 “ <span class ='pull-right'>” 。$ reply &#95; date
。 “ <b class ='下拉菜单'>
<small> <span class ='btn btn-xs btn-danger dropdown-toggle pull-right'data-toggle ='dropdown'>
<span class ='glyphicon glyphicon-edit'> </ span> </ span>
<ul class ='dropdown-menu'>“ 。$ reply &#95; delete&#95; button
。 “ <li> <a class ='glyphicon glyphicon-warning-sign'href ='report.php?u =” 。$ reply &#95; author。“'>报告</a> <li> </ ul> </ span>”
。 “ </ small> </ b> <br> <legend>” 。 html &#95; entity&#95; decode($ reply&#95; data)。“ </ legend> <br> </ div>”;
if ($ author == $ &#95; SESSION ['uname'] || $ account&#95; name == $&#95; SESSION ['uname']){
$ statusdeletebutton = '<li>'
。 '<a href =“#” type =“' 。$ updateid 。'” class =“ delete&#95; 4_session hidden&#95; text&#95; delete&#95;' 。$ updateid 。'glyphicon glyphicon-垃圾删除&#95;回复&#95; BTN”标题= “删除该状态和该国”>删除</A> </ LI>' ;
$ edit &#95; btn ='<li>'
。 '<a href =“#” attr =“' 。$ updateid 。'” type =“' 。$ updateid 。'” class =“ edit&#95; 4_session hidden&#95; text&#95; edit glyphicon glyphicon-pencil” title =“编辑此状态”>编辑</a> </ li>' ;
}
$ status &#95; list = $ statusui&#95; edit。'<div attr =“'。$ updateid。'” type =“'。$ updateid。'” class =“ statusboxes status&#95;'。$ updateid 。” jumbotron“>“
。 '<h3 style =“ color:black; margin-bottom:5px; margin-top:5px;” class =“ pull-left”>'
。 '<div id =“' 。$ updateid 。'” class =“ title&#95; s_2copy” value =“' 。 html &#95; entity&#95; decode($ title)。'”>'。html&#95 ; entity&#95; decode($ title)。'</ div> </ h3>'
。 '<span class =“ pull-right”>'
。 '<div class =“ dropdown”>'
。 '<button type =“ button” class =“ btn btn-danger dropdown-toggle” data-toggle =“ dropdown”>'
。 '<span class =“ glyphicon glyphicon-edit”> </ span> </ button>'
。 '<ul class =“ dropdown-menu”>'
。$ edit &#95; btn。” '。$ statusdeletebutton。'</ ul> </ div> </ span> <br> <hr>'
。 '<legend> <span class =“ data&#95; s_2copy” type =“' 。$ updateid 。'”>'
。html &#95; entity&#95; decode($ data)。'</ span> <br> <br> </ legend> <b style =“ text-align:right; color:black;”> <small>发布者:-<a href="home.php?u='.$author.'">'。$ author。'</a>'。$ post&#95; date。'</ small> </ b>'
。 '<br> <p>' 。$ status &#95; reply。'</ p> <br>';
$ status &#95; list。='<textarea id =“ reply&#95; textarea&#95;'。$ updateid。'” class =“ status&#95; reply&#95;'。$ updateid。' input-custom2“ placeholder =” comment \'s“> </ textarea>'
。 '<button id =“ reply&#95; btn&#95;' 。$ updateid 。'“ attr =”' 。$ updateid 。'“ type =” b“ class =” btn btn-warning右向btn-sm答复&#95; btn答复&#95;' 。$ updateid 。'“>回复</ button> </ div>' ;
回声 “ $ status&#95; list” ;
} } }
答:意识到您需要使用左联接而不是内部联接。它将解决您的帖子重复评论的问题。