subhra subhra - 1 year ago 41
MySQL Question

How to fetch value from MySQL table based on array value using PHP and MySQL

I need one help .I need to fetch values from Database but it should depends on some data present inside an array using PHP and Mysql.I am explaining my code below.


The above array has some value which is also present inside table as one column which is given below.


id name supplier_id city

1 Rocky 1 bbsr

2 Raj 4 CTC

3 Moynk 6 bLS

4 Nilima 1 CTC

5 Biswa 2 BBSR

6 Deepak 12 NPR

7 Rajua 13 OMP

8 Girija 10 CTC

9 Joyes 2 KKR

In the above table the array value are present inside column
here i need query to fetch all values whose
are belongs to that array and that also in descending order.Please help me.

Answer Source

You can use an IN clause for this. First of all take your array and implode it on , so you get a list of numbers you want to search against. Then provide that list to that IN clause and get your results.

$query="SELECT * FROM db_user WHERE supplier_id IN ($values) ORDER BY supplier_id DESC";

Now run that query