Query SQL values in php

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.