tags:

views:

52

answers:

1

I get this error below and was wondering what is it? And what can I do to fix it?

You have an error in your SQL syntax; check the manual that corresponds
to your MySQL server version for the right syntax to use near 
   ') AND articles_comments.user_id IN()' 
at line 6

Here is my MySQL code.

$u_id2 = array();
$dbc = mysqli_query($mysqli,"SELECT users.user_id FROM users WHERE users.active IS NOT NULL OR users.deletion = 1");
if(!$dbc){
    print mysqli_error($mysqli);
} else {
    while($row = mysqli_fetch_assoc($dbc)) {
        $u_id2[] = $row['user_id'];
    }
}

$bad_com_id2 = array();
$dbc = mysqli_query($mysqli,"SELECT articles_comments.comment_id
                             FROM articles_comments 
                             WHERE articles_comments.user_id IN(" . implode(",", $u_id2) . ") 
                             AND articles_comments.parent_comment_id = 0");
if(!$dbc){
    print mysqli_error($mysqli);
} else {
    while($row = mysqli_fetch_assoc($dbc)) {
        $bad_com_id2[] = $row['comment_id' ];
    }
}

$com_id2 = array();
$ucom_id2 = array();
$dbc = mysqli_query($mysqli,"SELECT articles_comments.comment_id, articles_comments.user_id FROM articles_comments WHERE articles_comments.article_id = '" . $article_id . "'");
if(!$dbc){
    print mysqli_error($mysqli);
} else {
    while($row = mysqli_fetch_assoc($dbc)) {
        $com_id2[] = $row['comment_id' ];
        $ucom_id2[] = $row['user_id'];
    }
}   

$comments_to_be_displayed2 = array_diff($ucom_id2, $u_id2);

$new_com_id2 = array();
$dbc = mysqli_query($mysqli,"SELECT articles_comments.comment_id 
                             FROM articles_comments 
                             INNER JOIN users ON articles_comments.user_id = users.user_id 
                             INNER JOIN users_articles ON articles_comments.article_id = users_articles.id
                             WHERE articles_comments.article_id = '" . $article_id . "'
                             AND articles_comments.comment_id IN(" . implode(",", $com_id2) . ") 
                             AND articles_comments.user_id IN(" . implode(",", $comments_to_be_displayed2) . ")");
if(!$dbc){
    print mysqli_error($mysqli);
} else {
    while($row = mysqli_fetch_assoc($dbc)) {
        $new_com_id2[] = $row['comment_id' ];
    }
}
+1  A: 

IN() is your problem. You need to have values there.

You could either test if you have values and exclude the condition from PHP when you generate the query string or use a trick:

IN(XYZ, " . implode(",", $u_id2) . ")

, where XYZ is a value that you are sure will never be used for user_id (maybe -1 if the column is a standard autoincremented primary key).

See here for reference: http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html#function_in

Alin Purcaru
+1. Good catch. Can we not use an empty string like `''` for your dummy entry ? I don't have access to MySQL.
codaddict
For good performance you should use the same data-type as the other values. I just gave an example, how and if it can be used depends from case to case.
Alin Purcaru