how to rectify cardinality violation error message in php&mysql

Tuesday, November 3, 2015

Im actually trying to get values for a commenting system with the following 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
/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




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. 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>

Remove X";
}




$status_replies="
Reply by:-".$reply_author.""
. "".$reply_date
. "

"
. "

". html_entity_decode($reply_data)."
";


}

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.'
'
. '

'
. '
'.html_entity_decode($title).'
</h3>'
. '<span class="pull-right">'
. '</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;

}

}

0 comments:

Post a Comment