Select the value included in the row

$string = 'US|UK|AUS|CA'; 

I'm trying to do something like this

 SELECT username FROM table WHERE country (included in $string) 

Any idea?

+4
source share
6 answers

Assuming your string is cleared, I would implode string in the array and use implode to create the statement. You should not store multiple values ​​in a row. What are arrays for?

 $string = 'US|UK|AUS|CA'; $string = explode('|', $string); $stmt = "SELECT ... country IN ('" . implode("', '", $string) . "')"; 

Gives out

 SELECT ... country IN ('US', 'UK', 'AUS', 'CA') 
+2
source

just explode your line with "|" to get an array, and then explode that array with "," to get a string.

 $string = 'US|UK|AUS|CA'; $string=explode("|",$string); $new_string=implode("', '",$string); 

then just run a query like this

 select username from table where country IN ('".$new_string."'); 
+2
source

Try it.

 SELECT username FROM table WHERE instr($string,country) > 0 ; 
+1
source

SELECT username FROM table WHERE country in('US', 'UK', 'AUS', 'CA')

0
source

If they are different ... you can try using Array

Sort of:

 $countries= array('a','b','c'); 

then you go on

 $q="SELECT username FROM table WHERE country in(".implode("','",$countries).") 

The imploit converts the keys of the array into a single line, divided ,

0
source

$ string = 'UK' OR 'US' OR 'AUS' OR 'CA'

-1
source

Source: https://habr.com/ru/post/1486481/


All Articles