Php queries the SQL value and then queries the problem Table B
Id title
1 Hello
2. Hi!
3 Hello everyone
$ Arr = hello, hello, everyone; separated by commas (,)
Then I want to query the title id of Table B through the value of $ arr.
How to write a query?
The effect is probably as follows:
$ Arr = hello, hello, everyone;
SELECT * from B table WHERE title = $ arr
Output result:
Hi, my id is 1
Hi, my id is 2
Hi, my id is 3
Reply to discussion (solution)
Hello, everyone. use $ array = implode (',', $ arr). The functions are separated by commas, then put it into an array, such as $ array.
Then SELECT * from B table WHERE title in ($ array );
Then use foreach to traverse and retrieve the result in the format.
How can I fix the missing quotation marks? SELECT * from B table WHERE title in ($ array); it should be in (' ', '',' '), now yes (Hello, I am good, hello everyone)
Create temporary table T select 1 as id, 'Hello, 'as title union select 2, 'Hello, 'Union select 3, 'Hello,'; select * from T where find_in_set (title, 'Hello, everyone ')
Id title 1 Hello 2 Hello 3 hello everyone
It can also be sorted
Select * from T where find_in_set (title, 'Hello, everyone ') order by find_in_set (title, 'Hello, everyone ');
Id title 2 Hello, 1 Hello, 3 Hello, everyone
"; For ($ I = 1; $ I <= sizeof ($ array); $ I ++) {$ array [$ i-1] = '\''. $ array [$ i-1]. '\ ''';} print_r ($ array); echo"
"; $ Array = implode (',', $ array); $ str =" select * from Table B where titile in ($ array); "; echo $ str;?>
The running result is
Array ([0] => Hello, [1] => Hello, everyone [2] => Hello, everyone)
Array ([0] => 'Hello, '[1] => 'Hello,' [2] => 'Hello, everybody ')
Select * from Table B where titile in (' ', '',' ');
I made a mistake last night. I used explode to help you.
$ Arr = "Hello, hello, everyone"; $ array = explode (',', $ arr); print_r ($ array); echo"
"; For ($ I = 1; $ I <= sizeof ($ array); $ I ++) {$ array [$ i-1] = '\''. $ array [$ i-1]. '\ ''';} print_r ($ array); echo"
"; $ Array = implode (',', $ array); echo $ array; echo"
"; $ Str =" select * from Table B where titile in ($ array); "; echo $ str;
The separator string is explode, and the merged array is implode. after the separator is separated, quotation marks are added to both sides of each bit, and then merged again.
The running result is
Array ([0] => Hello, [1] => Hello, everyone [2] => Hello, everyone)
Array ([0] => 'Hello, '[1] => 'Hello,' [2] => 'Hello, everybody ')
'Hi, 'hi,', 'Hello'
Select * from Table B where titile in (' ', '',' ');
I don't know if it meets your requirements.