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
https://forums.phpfreaks.com/topic/152670-odd-select-query/
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
https://forums.phpfreaks.com/topic/152670-odd-select-query/#findComment-801764
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.