- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我实际上正在尝试使用以下 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'";
但是出现如下错误:
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[21000]: Cardinality violation: 1222 The used SELECT statements have a different number of columns' in
/opt/lampp/htdocs/project-chg/example.php:21 Stack trace: #0
/opt/lampp/htdocs/project-chg/example.php(21): PDOStatement->execute()
#1 {main} thrown in /opt/lampp/htdocs/project-chg/example.php on line 21
那么我应该怎么做才能纠正这个错误以及如何将评论显示到适当的帖子?[编辑]:感谢指出差异 @Jakar 现在我在研究网络上的其他资源后遇到了一个新问题,我做了一个连接并且结果即将到来但现在的问题是结果似乎正在打印所有帖子。而不是应该附在帖子上的帖子。好像我搞砸了循环你能为我指出正确的循环吗???这是代码:
<?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;
}
}
最佳答案
当您使用 UNION
时,它会合并结果集,为此,每个结果集的列数必须相同。因此,您不应SELECT *
,而应SELECT col1,col2,col3
或任何您的列名。 (无论如何,使用 SELECT *
是一种不好的做法,因为它会降低代码的可读性并使 PHP 与数据库模式更紧密地耦合)。
我不知道你的表结构是什么样的,所以很难给出一个完美的答案,但我建议运行两个查询:
$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';"
否则,如果您确实需要(或更喜欢)UNION
,那么可以这样:
SELECT name,content,id FROM updates...
UNION
SELECT name,content,id FROM comment_update...
因为这样你在两个选择上有相同数量的列。
此外,在与 char/varchar
列进行比较时,最好使用 LIKE
而不是 =
。
关于php - 如何纠正 php mysql 中的基数违规错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33502395/
我想澄清一些关于违反 MVVM 的问题。因此,我创建了一个解决方案,其中包含一些项目来演示这些案例。 这是解决方案的定义(项目): View (它是一个 WPF 类库,显然它有 View ) View
这个比较器方法有什么问题? 我已阅读: Java error: Comparison method violates its general contract 并理解,如果 c1 > c2,且 c2
public Integer getMaxCount(String id,String type) { String maxCount ="SELECT MAX(ID) AS COUNT FR
代码如下 #include int main() { auto [a] = std::make_tuple(1); return [a]() -> int { return a;
我正在尝试开发一个应用程序,该应用程序将显示缩放以适合屏幕大小的任意图像。但是,在 EDT 上缩放图像会显着降低 UI,因此我决定使用以下代码在后台线程中缩放图像。 final Runnabl
无论如何,是否可以在 Grails 项目中自动查找 I18N 违规?例如, Enabled: 应该被标记,因为它没有使用 获取标签值。 如果 codenarc 对此有规则,那就太好了,但我认为没有。
我想从 Sonar 中排除一些方法(用于分析目的)。 实际上是自动生成的equals()和hashCode()。没有使用大括号等,我并不总是想修复它们。有没有办法只排除某些方法的范围? 最佳答案 您可
我有一些看起来像这样的代码(我用通用变量替换了我的业务变量): Map map = new HashMap(); for (int i = 1; i < 10; i++) { String s
在使用 IList> 时作为方法声明中的参数类型发生 FXCop 违规 It doesnt nest generic type IList> 我该如何解决? 最佳答案 原因是: A nested ty
我有以下代码,我得到了 PMD 违规 SuspiciousEqualsMethodName。 我想知道为什么这会被视为违规? private boolean areEquals(final Objec
令我惊讶的是,Java 有时可以为您检查比较器契约。 例如,当你写一个不遵循传递性的顺序关系时,你得到 java.lang.IllegalArgumentException: Comparison m
我们有一个头文件,其中包含各种浮点精度的一些残差: template struct rsdTarget { static const double value; }; template <> c
我正在尝试更新一篇博客文章,但我从数据库部分收到了唯一的关键错误,然后我没有使用模型并直接访问 ORM,但又没有成功。 这是我具体要编辑的路线 Route::get('/getedit/{slug}'
需要帮助解决 Sonar 问题。我们正在使用一些在 Maven 公共(public)存储库中不存在的第三方 jar。我将它的依赖定义为: api
我开始使用 CheckThreadViolationRepaintManager 来检测 EDT 违规。 它提示: partner = getParameter("partner",generateP
我有 ViewState POJO 类,它们的构造函数带有许多参数。问题是 PMD 对它们抛出 ExcessiveParameterList 违规。 现在,我试图抑制所有以 ViewState.jav
PMD 失败:...规则:UnusedPrivateMethod 优先级:3 避免未使用的私有(private)方法,例如“printMyString(String)” private void an
我写了一个 lisp 程序,它有两个参数,一个目的地和一个 BST 形式的 map 。它在 BST 中搜索目标号码,如果找到目标则打印 (found: path)。如果未找到,则应该打印(未找到:目的
我有以下 Java 方法: private int calculate() { return (bytes[0] & 0xff) + ((bytes[1] & 0xff) << 8); } P
我一直在思考以下问题。考虑两个文件: A.cpp: template void g(T) {} inline void f() { g(1); } B.cpp: template void g(T)
我是一名优秀的程序员,十分优秀!