[英]how to rectify cardinality violation error message in php&mysql
Im actually trying to get values for a commenting system with the following sql: 我实际上尝试使用以下sql获取评论系统的值:
$sql="select * from updates where account_name=:either and type IN ('a', 'c') "
. " union "
. "select * from comment_update where os_id=:statusid and type='b'";
but the following error occurs: 但是发生以下错误:
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[21000]: Cardinality violation: 1222 The used SELECT statements have a different number of columns' in 致命错误:消息为“ SQLSTATE [21000]”的未捕获异常“ PDOException”:基数违反:1222在其中使用的SELECT语句具有不同的列数
/opt/lampp/htdocs/project-chg/example.php:21 Stack trace: #0 /opt/lampp/htdocs/project-chg/example.php:21堆栈跟踪:#0
/opt/lampp/htdocs/project-chg/example.php(21): PDOStatement->execute() /opt/lampp/htdocs/project-chg/example.php(21):PDOStatement-> execute()
#1 {main} thrown in /opt/lampp/htdocs/project-chg/example.php on line 21 在第21行的/opt/lampp/htdocs/project-chg/example.php中抛出#1 {main}
So what should i do to rectify this error and how to display the comments to appropriate posts? 那么我该怎么做才能纠正此错误,以及如何在适当的帖子中显示评论? [edit]: thanks for pointing the difference @Jakar now i face a new problem after researching other resources on the web, i did a join and the results are coming but the problem now is the results seem to be printing for all posts. [edit]:感谢指出差异@Jakar现在,我在研究网络上的其他资源后遇到了一个新问题,我加入了连接,结果即将来临,但是现在的问题是,所有帖子的结果似乎都在打印。 and not to posts where they should be attached. 而不是张贴在应附加的位置。 seems like i screwed up the loop can you point the right loop for me.??? 好像我搞砸了循环,你能为我指出正确的循环吗??? here is the code: 这是代码:
<?php
$status2view=$project->statusView($_SESSION['uname']);
foreach($status2view as $row){
//print_r($row);
$status_id=$row['update_id'];
// SELECT * FROM Product,Color WHERE Product.Name = Color.Name
$sql="select update_id,update_body, time, title, author,type from updates where account_name=:either and type IN ('a', 'c') "
. " union "
. "select comment_id, author,time,comment_body,os_id ,type from comment_update where os_id=:statusid and type='b'";
$sql1="select comment_update.comment_id, comment_update.author,comment_update.time,comment_update.comment_body,comment_update.os_id ,comment_update.type from updates,comment_update where comment_update.os_id like updates.update_id ";
$stmth=$conn->prepare($sql1);
// $stmth->bindparam(":either",$_SESSION['uname']);
//$stmth->bindparam(":statusid",$status_id);
$stmth->execute();
$status_reply= $stmth->fetchAll(PDO::FETCH_ASSOC);
?>
</div>
<div class="col-lg-8">
<?php
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'] || $account_name==$_SESSION['uname']) {
$reply_delete_button.="<li><span id='$status_reply_id' class='delete_reply_btn glyphicon glyphicon-remove'><a href='#' title='Delete this comment'>Remove X</a></span></li>";
}
$status_replies="<div class='replyboxes pull-left reply_".$status_reply_id."'><b>Reply by:-<a href='search_results.php?u=".$reply_author."'>".$reply_author."</a>"
. "<span class='pull-right'>".$reply_date
. "<b class='caret'>
<small><span class='btn-xs btn-danger dropdown-toggle pull-right' data-toggle='dropdown' aria-expanded='true' ><span class='glyphicon glyphicon-edit'></span>
<ul class='dropdown-menu'>".$reply_delete_button
. "<li><a href='#' class='hidden_text_area glyphicon glyphicon-pencil reply_".$status_reply_id."' title='Edit this comment' >Edit</a></li>"
. "<li><a href='report.php?u='".$reply_author."'>Report</a><li></ul>"
. "</span></span></small></b><br><legend>". html_entity_decode($reply_data)."</legend><br></div>";
}
foreach($status2view as $row){
$updateid=$row['update_id'];
$account_name=$row['account_name'];
$os_id=$row['os_id'];
$author=$row['author'];
$post_date=$row['time'];
$title= stripslashes($row['title']);
$data= stripslashes($row['update_body']);
$statusdeletebutton='';
//insert_status_ui script to get message.
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="search_results.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;
}
}
When you use a UNION
, it combines the result sets, and in order to do that, the number of columns on each has to be the same. 当您使用UNION
,它会组合结果集,并且要这样做,每个列的列数必须相同。 Thus, you should not SELECT *
, but SELECT col1,col2,col3
or whatever your column names are. 因此,您不应SELECT *
,而应SELECT col1,col2,col3
或任何列名。 (it's bad practice to use SELECT *
anyway, because it lowers readability of code and more tightly couples your PHP to your database schema). (无论如何,使用SELECT *
不好的做法,因为它降低了代码的可读性,并使PHP与数据库模式更紧密地耦合在一起)。
I don't know what your table structures are like, so it's awfully hard to give a perfect answer, but I'd suggest running two queries: 我不知道您的表结构是什么样的,因此很难给出一个完美的答案,但是我建议运行两个查询:
$sql =
"SELECT column_list... FROM updates
WHERE account_name LIKE :either AND type IN ('a','c');"
."SELECT column_list... FROM comment_update
WHERE os_id=:statusid AND type LIKE 'b';"
Otherwise, if you do indeed need (or prefer) the UNION
, then something like: 否则,如果您确实确实需要(或希望使用) UNION
,则类似于:
SELECT name,content,id FROM updates...
UNION
SELECT name,content,id FROM comment_update...
Because that way you have the same number of columns on both selects. 因为这样,两个选择上的列数都相同。
Also, it's better to use LIKE
than =
when comparing with a char/varchar
column. 另外,与char/varchar
列进行比较时,最好使用LIKE
不是=
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.