Jump to content

Odd Select Query


tmh766

Recommended Posts

Hey

 

I can't seem to figure out how to write this query.

 

I have a PHP variable set to, for example:

$match='a,b,c';

and want to check if a MySql field's value is within the variable match, not the other way around like usual.

 

So if theMySql field had a value of "a", that row would be returned because the field's value is a substring of the variable match.

 

Tim

 

 

Link to comment
Share on other sites

You could use IN.

 

 

Except for your string would need to be formatted differently.

 

 

Example query:

 

SELECT * FROM table1 WHERE field1 IN('a', 'b', 'c');

 

 

So, if you had a comma separated list in PHP, you could do something like this:

 

"SELECT * FROM table1 WHERE field1 IN ('" . implode("', '", explode(",", $input)) . "');"

 

 

Edit:  By the way, it would be wiser to explode() the array and then mysql_real_escape_string all of the pieces before putting it back together.

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.