user5443928
user5443928

Reputation:

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.

$arr=['1','2','4','5','12','13'];

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

db_user:

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 supplier_id here i need query to fetch all values whose supplier_id are belongs to that array and that also in descending order.Please help me.

Upvotes: 0

Views: 766

Answers (1)

Hanky Panky
Hanky Panky

Reputation: 46900

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.

$arr=['1','2','4','5','12','13'];
$values=implode(",",$arr);
$query="SELECT * FROM db_user WHERE supplier_id IN ($values) ORDER BY supplier_id DESC";

Now run that query

Upvotes: 4

Related Questions