2
votes

Query FailedYou have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'comment_status = 'Approved' ORDER BY comment_id DESC' at line 1

This is my code

<?php
    $query = "SELECT * FROM comments WHERE comment_post_id = {$the_post_id}";
    $query .= "AND comment_status = 'Approved' ";
    $query .= "ORDER BY comment_id DESC";
    $select_comment_query = mysqli_query($connection, $query);
    if(!$select_comment_query){
        die('Query Failed'. mysqli_error($connection));
    }
    while ($row = mysqli_fetch_array($select_comment_query)) {
        $comment_date = $row['comment_date'];
        $comment_content = $row['comment_content'];
        $comment_author = $row['comment_author'];
?>

<!-- Comments -->
<div class="media">
    <a class="pull-left" href="#">
        <img class="media-object" src="http://placehold.it/64x64" alt="">
    </a>
    <div class="media-body">
        <h4 class="media-heading"><?php echo $comment_author; ?>
            <small><?php echo $comment_date; ?></small>
        </h4>
        <?php echo $comment_content; ?>
    </div>
</div>

<?php } ?>

If I disable the following codes, it works

    $query .= "AND comment_status = 'Approved' ";
    $query .= "ORDER BY comment_id DESC";

Thank you

1
I thin one more space required after {$the_post_id} or before AND in second line. - Maha Dev
Right before you call mysqli_query(), echo the contents of $query to see if you're passing the query you think you're passing. - Darwin von Corax
its not good to mark first to someone and then mark another one. - Anant Kumar Singh
And i think my answer is more descriptive and more good suggestion is there. No offense, i deleted my answer - Anant Kumar Singh

1 Answers

2
votes

Try including a space:

$query .= " AND comment_status = 'Approved' ";

As a note: if you want to debug these types of problems, then output the query string before running it. Often the problem is obvious.