Jump to content

MYSQL in_array?


EchoFool

Recommended Posts

Hey

 

I am wondering how i can check in_array values in SQL with two different types of checks. I want to look for rows which have "all" the values... OR rows which have at least one of the values... here is an example of my array:

 

 

Currently i use:

 

<php
//fieldname in this instance =  1,2
//Syntax in my query
FieldName In ('1,2,3,4')
?>

 

But im not sure if that means its checking if its at least one of the 4 numbers... or if its checking if FieldName contains all 4 numbers...?

Does any one know the syntax for both circumstances?

Link to comment
https://forums.phpfreaks.com/topic/252276-mysql-in_array/
Share on other sites

Hi

 

In the case you have there it is going to look for '1,2', and not find it as it the only option is the string '1,2,3,4'.

 

You would need to explode Fieldname into an array, and check for each member of the array being withing (1,2,3,4) without the quotes around them.

 

Something like:-

 

$sql = 'SELECT * FROM SomeTable WHERE '.explode(' IN (1,2,3,4) OR ',FieldName).' IN (1,2,3,4) ';

 

(note the above will screw up if FieldName is empty).

 

All the best

 

Keith

Link to comment
https://forums.phpfreaks.com/topic/252276-mysql-in_array/#findComment-1293403
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.