0

Hi :) I write some code for me and I need to compare two columns from different tables.

I have table blog_posts with postID and I have table blog_comment with also have postID.

I write something like this for start:

<?php
$stmt2 = $db->prepare('SELECT comment_sender_name, comment_date, comment 
                       FROM blog_posts, blog_comment 
                       WHERE blog_posts.psotID = blog_comment.postID');
$stmt2->execute(array(
    ':comment_sender_name' => $comment_sender_name,
    ':comment_date' => $comment_date,
    ':comment' => $comment
));
if ($row['blog_posts.psotID'] == $row['blog_comment.postID']) {
    echo '<p>' . $comment_sender_name . '</p>';
    echo '<p>' . $comment_date . '</p>';
    echo '<p>' . $comment . '</p>';
} else ($row['blog_posts.psotID'] == '') {
    header('Location: ./');
    exit;
}
?>

And this is the error I get :

Parse error: syntax error, unexpected '{' in /home/pomarex1/domains/dev.automax-rent.eu/public_html/comments-loop.php on line 12

Mickaël Leger
  • 3,426
  • 2
  • 17
  • 36

2 Answers2

0

I can see two errors in your code (if this is not a typo one):-

  1. In the query where you have written "WHERE blog_posts.psotID = blog_comment.postID" , it should be "blog_posts.postID" and not "blog_posts.psotID" because psotID is not a field in your table .

  2. Like what others say , your else is wrong , either you write

    else{ header('Location: ./'); exit; }

OR

else if($row['blog_posts.psotID'] == ''){
    header('Location: ./');
    exit;
}
  1. And thirdly , I will suggest to write an inner join query rather than calling the data directly .
PHP Web
  • 257
  • 3
  • 8
0

This is the code to fetch data from two different tables and you can compare them. Here I've used PDO method

<?php
    require_once('init.php');
    $con = $db->prepare("SELECT * FROM blog_posts");
    $con->execute();
    $con2 = $db->prepare("SELECT * FROM blog_comment");
    $con2->execute();
    while(($post_row = $con->fetch(PDO::FETCH_ASSOC)) && ($comment_row = $con2->fetch(PDO::FETCH_ASSOC))){
            if($post_row['postID'] == $comment_row['postID']){
                echo $post_row['postID'];
        }
    }

?>
bairavand
  • 335
  • 3
  • 11