Jump to content

mysql search using php array


severndigital

Recommended Posts

OK so i have a search query for Mysql that looks like this.

 

SELECT *
FROM prod_list
WHERE name LIKE '%$search%'
OR short_desc LIKE '%$search%'
OR description LIKE '%$search%'
AND access_levels LIKE '%$user_level%'
ORDER BY {$orderby} {$ordermeth}
LIMIT {$startRange},{$endRange}";

 

I would like to add to it and search through a php array against another field.

 

$items = array(1,10,11,15,17);

$sql = "SELECT *
FROM prod_list
WHERE name LIKE '%$search%'
OR short_desc LIKE '%$search%'
OR description LIKE '%$search%'
AND access_levels LIKE '%$user_level%'
AND cat_id IN ......HOW DO I SEARCH AGAINST THE ARRAY????
ORDER BY {$orderby} {$ordermeth}
LIMIT {$startRange},{$endRange}";

 

how do i search against the array??

 

I have tried the following with no success.

$items = implode(',',$items);

$sql = "SELECT * ....
AND cat_id IN '$items'
....";

//and also
$sql = "SELECT * ....
AND cat_id ('".join("','", $items)."')
....";

 

Both had bad results.

 

 

 

 

 

 

 

Thanks in advance,

C

Link to comment
Share on other sites

Try using implode()

 

$sql = "SELECT *
FROM prod_list
WHERE name LIKE '%$search%'
OR short_desc LIKE '%$search%'
OR description LIKE '%$search%'
AND access_levels LIKE '%$user_level%'
AND cat_id IN (". implode(',', $items) .")
ORDER BY {$orderby} {$ordermeth}
LIMIT {$startRange},{$endRange}";

Link to comment
Share on other sites

I tried that originally but it doesn't seem to work.

 

//the cat_id for it items in this test should be 13

//the array would be
$items = array(10,11,12,18);

when I search i get the results, but they all have a cat_id of 13 where it should be returning Zero results, since they do not match the array

 

Any ideas??

 

Link to comment
Share on other sites

Your ORs and ANDs are getting mixed up.

 


$sql = "SELECT *
FROM prod_list
WHERE ( name LIKE '%$search%'
OR short_desc LIKE '%$search%'
OR description LIKE '%$search%' )
AND access_levels LIKE '%$user_level%'
AND cat_id IN (". implode(',', $items) .")
ORDER BY {$orderby} {$ordermeth}
LIMIT {$startRange},{$endRange}";

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.