Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is wrong with this sql query?

Tags:

sql

mysql

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!

like image 977
ajsie Avatar asked Feb 28 '23 14:02

ajsie


1 Answers

$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))";
like image 60
too much php Avatar answered Mar 08 '23 17:03

too much php