tags:

views:

73

answers:

3

what is wrong with this sql query. i cant figure it out.

    $query = "SELECT *
    FROM tagPairs
    WHERE (tag1Id IN ($tag1Id, $tag2Id))
    AND (tag2Id IN ($tag1Id, $tag2Id))";

error code:

Couldn't execute query: 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 (tag2Id IN (, ))' at line 3

thanks in advance!

+3  A: 

$tag1Id and $tag2Id are empty.

That's why your error says (tag2Id IN (, )).

Peter Lang
+1  A: 

Your $tag1Id and $tag2Id are empty strings. Assign a value to them and it should work fine.

Also, selecting * is a bad idea. Select the columns you need explicity.

Mark Byers
what if i dont need any column. i just wanted to check if a row exists in a table. what should i use instead of * then?
weng
in that case use "select 1 where exists (select * from ... )"
geofftnz
select count(*) ...
bromfiets
+4  A: 

$tag1Id and $tag2Id are both null, or empty strings. The simplest solutions is probably to explicitly cast them into numerical values:

$tag1Id = intval($tag1Id);
$tag2Id = intval($tag2Id);
$query = "SELECT *
    FROM tagPairs
    WHERE (tag1Id IN ($tag1Id, $tag2Id))
    AND (tag2Id IN ($tag1Id, $tag2Id))";
too much php