Current location: Hot Scripts Forums » Programming Languages » PHP » search MySQL field for any item from array


search MySQL field for any item from array

Reply
  #1 (permalink)  
Old 06-23-09, 01:36 PM
m_abdelfattah m_abdelfattah is offline
Newbie Coder
 
Join Date: Sep 2004
Location: Alexandria, Egypt.
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
search MySQL field for any item from array

Now, I've MySQL field called `catgegory_ids`, if one row contains (1,5,23,45) . how can I make a search if any of these categories (1,5) are found ??

I thought of making the following:
PHP Code:

$category_ids = array(1,2,5,10);

foreach(
$category_ids AS $category_id) {
$query .= "OR `category_ids` LIKE `%" $category_id "%' ";

But I feel it's stupid way !
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 06-23-09, 01:46 PM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,074
Thanks: 11
Thanked 88 Times in 83 Posts
PHP Code:

"... WHERE `category_ids` IN(" implode(', '$category_ids) . ")" 

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 06-23-09, 03:19 PM
m_abdelfattah m_abdelfattah is offline
Newbie Coder
 
Join Date: Sep 2004
Location: Alexandria, Egypt.
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by Nico View Post
PHP Code:

"... WHERE `category_ids` IN(" implode(', '$category_ids) . ")" 

now I add the value
Code:
1,2,3,4,56
to `category_ids` and here're the results
PHP Code:

SELECT From `itemsWHERE `category_idsIN (1); 

Working.

PHP Code:

SELECT From `itemsWHERE `category_idsIN (1); 

Not working

PHP Code:

SELECT From `itmesWHERE `category_idsIN (56); 

Not working
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 06-26-09, 06:42 PM
Jcbones Jcbones is offline
Aspiring Coder
 
Join Date: Mar 2009
Location: North Carolina, USA
Posts: 516
Thanks: 5
Thanked 47 Times in 44 Posts
Perhaps you should spell "items" right...

And your first and second query are the same, only one works but the other doesn't?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #5 (permalink)  
Old 06-27-09, 05:13 AM
m_abdelfattah m_abdelfattah is offline
Newbie Coder
 
Join Date: Sep 2004
Location: Alexandria, Egypt.
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by Jcbones View Post
Perhaps you should spell "items" right...

And your first and second query are the same, only one works but the other doesn't?
BTW, the real table name is not items I just changed it here while writing the reply, so it's not a miss-spell !

and 2nd query which is not working , should be
Code:
SELECT * From `items` WHERE `category_ids` IN (1,2);
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #6 (permalink)  
Old 06-28-09, 01:48 PM
Jcbones Jcbones is offline
Aspiring Coder
 
Join Date: Mar 2009
Location: North Carolina, USA
Posts: 516
Thanks: 5
Thanked 47 Times in 44 Posts
There is no reason those queries shouldn't work.

Have you put them into mysql console to see if it returns an error message?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #7 (permalink)  
Old 06-29-09, 04:15 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,074
Thanks: 11
Thanked 88 Times in 83 Posts
I think the reason it doesn't work is:
Quote:
Originally Posted by m_abdelfattah View Post
if one row contains (1,5,23,45)
You're going to have to use regular expressions to have this work correctly. This isn't the best practice, though. You should use a third table to store the relations.

Anyway:
PHP Code:

"SELECT ... WHERE `category_ids` REGEXP '(^|,)(" implode('|'$category_ids) . ")(,|$)'" 

(Untested, but should work)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Superfish jquery menu problems with modification transcend2005 CSS 2 03-25-09 02:30 AM
display mysql data in columns? todayscoffee PHP 4 03-11-09 11:05 PM
Help making a certain field into a hyperlink from an echoed MySQL table cynebald PHP 5 05-11-06 11:20 PM
Declared Functions skipper23 PHP 4 12-17-03 11:06 AM
index page not showing up skipper23 PHP 3 12-15-03 02:10 PM


All times are GMT -5. The time now is 04:58 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.