Just in Mind
Just in Mind

Reputation: 130

retrieve Comma separated values in MySQL php with LIKE

Array
(
    [0] => Array
        (
            [id] => 1
        )

    [1] => Array
        (
            [id] => 16
        )

    [2] => Array
        (
            [id] => 21
        )

    [3] => Array
        (
            [id] => 22
        )

    [4] => Array
        (
            [id] => 2
        )
)

above array is an $id

$grp_id="";
foreach($id as $gid)
{
   $grp_id .=  " LIKE %".$gid['id']."% AND rj.applyto";
}

my query is

$sql="SELECT * FROM `table` j INNER JOIN `table2` rj ON j.eid = rj.eid WHERE rj.applyto LIKE $grp_id";

i did an mistake in my auery pls suggest me to coorect..

Upvotes: 0

Views: 728

Answers (2)

Alauddin Ansari
Alauddin Ansari

Reputation: 250

You just need to implode your ids with comma separated and use FIND_IN_SET for this like:

$ids = implode(',', $id_array);
$sql="SELECT * FROM `table` j INNER JOIN `table2` rj ON j.eid = rj.eid WHERE FIND_IN_SET(rj.applyto, $ids)";

Try this!

Upvotes: 0

John Conde
John Conde

Reputation: 219934

Do you really need to use LIKE here? 1 and 2 are so common I don't think it is what you want.

Assuming that is not what you really want, just use IN(). Much simpler.

// Create comma separated list of IDs
$ids = implode(',', $id);
// Use IN() in our WHERE clause
$sql="SELECT * FROM `table` j INNER JOIN `table2` rj ON j.eid = rj.eid WHERE rj.applyto IN($ids);

Upvotes: 3

Related Questions