Julian Posted February 23, 2009 Share Posted February 23, 2009 Hello guys, here's my situation: I stored values in array as follows (1,5,7,9). What I want to do is perform a search within that array, for example: WHERE field IN (array). Is that possible? Here's the code portion I want to fix: $query_com = sprintf("SELECT * FROM companias WHERE '$colname_producto' IN (id_compania) ORDER BY compania ASC"); Thanks Quote Link to comment https://forums.phpfreaks.com/topic/146597-solved-search-within-an-array/ Share on other sites More sharing options...
The Little Guy Posted February 23, 2009 Share Posted February 23, 2009 Untested, but this may be what you want... $arr = array(1,5,7,9); $query_com = "SELECT * FROM companias WHERE"; foreach($arr as $val){ $query .= " `$colname_producto` = '$val' OR"; } $query = trim($query 'OR'); $query .= "ORDER BY compania ASC"; mysql_query($query); Quote Link to comment https://forums.phpfreaks.com/topic/146597-solved-search-within-an-array/#findComment-769609 Share on other sites More sharing options...
Julian Posted February 23, 2009 Author Share Posted February 23, 2009 Thanks Little Guy I'm trying to complete a simple search. I get one of the numbers stored on the array (method=get) on the search form. Example: The search is for number 1. I want to look for all the records that contain "1" on the array stored on field: id_product. Each company have menu "id_product" (1, ,5, 7, 9). I hope I explained myself better this time. Thanks for the response. Quote Link to comment https://forums.phpfreaks.com/topic/146597-solved-search-within-an-array/#findComment-769619 Share on other sites More sharing options...
The Little Guy Posted February 23, 2009 Share Posted February 23, 2009 so... you want to search the array for the number one? or you want to search the database for the number one? Quote Link to comment https://forums.phpfreaks.com/topic/146597-solved-search-within-an-array/#findComment-769629 Share on other sites More sharing options...
trq Posted February 24, 2009 Share Posted February 24, 2009 $query_com = sprintf("SELECT * FROM companias WHERE id_compania IN ('%s') ORDER BY compania ASC", implode("','", $colname_producto)); Quote Link to comment https://forums.phpfreaks.com/topic/146597-solved-search-within-an-array/#findComment-769646 Share on other sites More sharing options...
samshel Posted February 24, 2009 Share Posted February 24, 2009 if u want to search a number in array you can use in_array() if u want to search in database then solution from thorpe will work for u Quote Link to comment https://forums.phpfreaks.com/topic/146597-solved-search-within-an-array/#findComment-769650 Share on other sites More sharing options...
Julian Posted February 24, 2009 Author Share Posted February 24, 2009 Nothing seems to work. Thanks for the responses. Here's a basic explanation of what I'm trying to achieve: I'm trying to search an array in the database. One of my fields has something like this: 1,2,22,224 So if I do: WHERE Value LIKE '%2%' It will return: 2,22,224 If I do: WHERE Value = '2' It will return nothing... How can I just get '2' ? Best regards Quote Link to comment https://forums.phpfreaks.com/topic/146597-solved-search-within-an-array/#findComment-770079 Share on other sites More sharing options...
sasa Posted February 24, 2009 Share Posted February 24, 2009 http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_find-in-set WHERE FIND_IN_SET('$colname_producto', id_compania) Quote Link to comment https://forums.phpfreaks.com/topic/146597-solved-search-within-an-array/#findComment-770105 Share on other sites More sharing options...
Julian Posted February 24, 2009 Author Share Posted February 24, 2009 You are the Man! This worked flawless. Thanks a lot! Quote Link to comment https://forums.phpfreaks.com/topic/146597-solved-search-within-an-array/#findComment-770114 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.