Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/146597-solved-search-within-an-array/
Share on other sites

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);

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.

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

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.