Search code examples
phpmysqlwhere-clausewhere-in

Trying to get data from mysql table via php with IN clause (favorites)


Here is how my table "User" looks:

Name Password Favorites

Test Test     1 2 3 4

And in my table "Data" I have

Comment ID
"Test"  2

I want the the user to be able to save a comment to its favorites, hence the first table, where I save all favorites in one row, so the table doesn't get too big. I try to get them all back by implode and IN clause. Right now it does not seem to work and I hope that maybe someone here could give me some useful input on how to conquer this problem :)

$favoritenstring = ($GET_["favoritenstring"]);

$query = "SELECT * FROM $table_id WHERE ID in ('" . implode("','",$favoritenstring) . "')";

Right now I am getting this error on the above query line: Invalid arguments passed


Solution

  • Your have to remove slashes from the string first:

    $string = stripslashes($favoritenstring);
    $query = "SELECT * FROM $table_id WHERE ID in " .$string;