Php 返回空结果的mysql查询
在下面的查询中,我试图将一个comments表连接到posts表Php 返回空结果的mysql查询,php,mysql,pdo,Php,Mysql,Pdo,在下面的查询中,我试图将一个comments表连接到posts表 $sql3=" select u.update_body , u.author , u.time , u.title , c.comment_body , c.os_id , c.author , c.time from updates as u join comment_update as c on c.os_id = :statusid
$sql3="
select u.update_body
, u.author
, u.time
, u.title
, c.comment_body
, c.os_id
, c.author
, c.time
from updates as u
join comment_update as c
on c.os_id = :statusid
and u.account_name = :session
and u.type = ('a'&'c')
or u.account_name = :friend
and u.type = ('a'&'c')
order
by u.time asc
, c.time desc";
$stmth=$conn->prepare($sql3);
$stmth->bindValue(":session",$_SESSION['uname']);
$stmth->bindValue(":friend",$friend);
$stmth->bindValue(":statusid",$updateid);
$stmth->execute();
它返回空结果,不知道我哪里出了错???。任何帮助都将不胜感激。
以下是我的数据库模式:
根据arsh的要求,我在这里发布了全部代码:
<?php
include "includes/dbconfig.inc.php";
$status_replies="";
$status_list="";
$statusui_edit="";
$isowner="";
$is_friend="";
$friends = array();
$stmt= $conn->prepare("select friend_one, friend_two from friends where "
. "(friend_one=:session OR friend_two=:session) and accepted='1'");
$stmt->bindparam(":session",$_SESSION['uname']);
$stmt->execute();
$status2view=$project->statusView($_SESSION['uname']);
foreach ($stmt->fetchAll(PDO::FETCH_ASSOC) as $i=> $r ) {
$r["friend_one"] == $_SESSION['uname'] ? $friends[]= $r["friend_two"] : $friends[] = $r["friend_one"];
$friend=$friends[$i];
foreach ($status2view as $val) {
$updateid=$val['update_id'];
//select all relevant comments using the following query and print it.
$sql1="select u.update_body,u.author,u.time,u.title,"
. "c.comment_body, c.os_id,c.author,c.time "
. "from updates as u join comment_update as c "
. "on c.os_id=:statusid and u.account_name = :session and u.type = ('a'&'c') or u.account_name=:friend and u.type = ('a'&'c') order by u.time asc,c.time desc";
$sql2=" select u.update_body,u.author,u.time,u.title,"
. "c.comment_body, c.os_id,c.author,c.time "
. "from updates as u join comment_update as c "
. "on c.os_id=u.update_id where u.account_name = :session and u.type = ('a'&'c') or u.account_name=:friend and u.type = ('a'&'c') order by u.time asc,c.time desc";
$stmth=$conn->prepare($sql2);
$stmth->bindValue(":session",$_SESSION['uname']);
$stmth->bindValue(":friend",$friend);
//$stmth->bindValue(":statusid",$updateid);
$stmth->execute();
$status_reply= $stmth->fetchAll(PDO::FETCH_ASSOC);
print_r($status_reply);
foreach ($status_reply as $row1) {
$status_reply_id=$row1['comment_id'];
$reply_author=$row1['author'];
$reply_d=htmlentities($row1['comment_body']);
$reply_data= stripslashes($reply_d);
$reply_osid=$row1['os_id'];
$reply_date=$row1['time'];
$reply_delete_button="";
if ($reply_author==$_SESSION['uname'] ) {
$reply_delete_button="<li><a href='#'type='".$status_reply_id."' class='delete_reply_btn glyphicon glyphicon-trash delete_reply_".$status_reply_id."' title='Delete this comment'> Remove</a></span></li>";
}
if ($reply_osid==$updateid && $isowner=="yes") {
$status_replies="<div class='replyboxes pull-left reply_".$status_reply_id."'>"
. "Reply by:- "
. "<a href='home.php?u=".$reply_author."'>".$reply_author."</a>"
. "<span class='pull-right'>".$reply_date
. "<b class='dropdown'>
<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_delete_button
. "<li><a class='glyphicon glyphicon-warning-sign' href='report.php?u=".$reply_author."'> Report</a><li></ul></span>"
. "</small></b><br><legend>". html_entity_decode($reply_data)."</legend><br></div>";
}
elseif ($reply_osid==$updateid && $is_friend==TRUE && $session_uname!=$f_uname) {
$status_replies.="<div class='replyboxes pull-left reply_".$status_reply_id."'>"
. "Reply by:-"
. "<a href='home.php?u=".$reply_author."'>".$reply_author."</a>"
. "<span class='pull-right'>".$reply_date."
<b class='dropdown'>
<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_delete_button
. "<li><a class='glyphicon glyphicon-warning-sign report_btn report_".$updateid."' type='".$updateid."' href='report.php?u=".$reply_author."'> Report</a><li></ul></span>"
. "</small></b><br>"
. "</span><br><legend>". html_entity_decode($reply_data)."</legend><br></div>";
}
else {
$status_replies.="";
}
$updateid=$row1['update_id'];
$author=$row1['author'];
$post_date=$row1['time'];
$title= stripslashes($row1['title']);
$data= stripslashes($row1['update_body']);
$statusdeletebutton='';
if ($author==$_SESSION['uname'] || $account_name==$_SESSION['uname']) {
$statusdeletebutton='<li>'
. '<a href="#" type="'.$updateid.'" class="delete_4_session hidden_text_delete_'.$updateid.' glyphicon glyphicon-trash delete_reply_btn" title="Delete this status and its replies">Remove</a></li>';
}
$status_list= $statusui_edit.'<div attr="'.$updateid.'" type="'.$updateid.'" class="statusboxes status_'.$updateid.' jumbotron">'
. '<h3 style="color:black; margin-bottom:5px; margin-top:5px;" class="pull-left">'
. '<div id="'.$updateid.'" class="title_s_2copy" value="'.html_entity_decode($title).'">'.html_entity_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">'
. '<li><a href="#" attr="'.$updateid.'" type="'.$updateid.'" class="edit_4_session hidden_text_edit glyphicon glyphicon-pencil" title="Edit this status" >Edit</a></li>'.$statusdeletebutton.'</ul></div></span><br><hr>'
. '<legend><span class=" data_s_2copy" type="'.$updateid.'" >'
. html_entity_decode($data).'</span><br><br></legend><b style="text-align:right; color:black;"><small>Posted by:- <a href="home.php?u='.$author.'">'.$author. '</a> '.$post_date.'</small></b>'
. '<br><p>'.$status_replies.'</p><br>';
$status_list= '<textarea id="reply_textarea_'.$updateid.'" class="status_reply_'.$updateid.' input-custom2" placeholder="comment\'s"></textarea>'
. '<button id="reply_btn_'.$updateid.'" attr="'.$updateid.'" type="b" class="btn btn-warning pull-right btn-sm reply_btn reply_'.$updateid.'">Reply</button></div>';
echo "$status_list";
} } }
首先,添加打印结果的php代码以查看结果,如下所示 您的查询中缺少
where
子句,而且c.os_id=:statusid上的在加入表时出错
在c.os\u id=c.comment\u id
上使用此我有位置,但您必须在c.comment\u id
的位置插入正确的列名,因为我看不出您是如何命名的:
$sql3="select u.update_body,u.author,u.time,u.title,"
. "c.comment_body, c.os_id,c.author,c.time "
. "from updates u left join comment_update c "
. "on c.os_id=c.comment_id WHERE u.account_name = :session or u.account_name=:friend and (u.type = 'a' or 'c') order by u.time asc,c.time desc";
$stmth=$conn->prepare($sql3);
$stmth->bindValue(":session",$_SESSION['uname']);
$stmth->bindValue(":friend",$friend);
$stmth->execute();
op发布错误时,他得到的错误声明其中一个值为空检查这些值是否需要有值且不为空,并删除$stmth->bindValue(“:statusid“,$updateid”)代码>因为您不再使用它,它会给您一个令牌和绑定参数错误:
$stmth->bindValue(":session",$_SESSION['uname']);
$stmth->bindValue(":friend",$friend);
虽然这可能是实际的解决方案,但你可能想解释你改变了什么——以及为什么必须改变,以便OP可以从中学习,而不是仅仅完成工作而不从中学习任何东西。@Qirel是的,我本来打算这么做的,但有紧急情况,不得不去解释it@shan2batman错误并将php代码放在打印结果的位置question@shan2batman您的查询中还有一个错误,让我更新我的answer@shan2batman如果您echo$sql3,请检查编辑回答中是否存在另一个错误代码>,它给出了什么?它输出查询并循环,因为它位于foreach循环@QirelYes中,当然它输出查询,但它看起来像什么?如果您看到查询的整个输出,有时更容易发现错误。@shan2batman我已更新了我的答案您在查询检查中有错误,您缺少澄清“或”含义所需的括号。另外,我不熟悉“('a'&'c')”语法。那是什么?您还没有(在查询中)定义表之间的关系。另外,虽然我完全同意您应该在生产和开发中使用准备好的语句,但如果您直接提交变量,则更容易弄清楚发生了什么
$stmth->bindValue(":session",$_SESSION['uname']);
$stmth->bindValue(":friend",$friend);