0

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.

2
  • please post your SELECT query Commented May 19, 2016 at 10:01
  • My guess is that @subhra doesn't know what a SELECT is... SELECT * FROM db_user WHERE supplier_id=X OR supplier_id=Y OR ... OR supplier_id=Z ORDER BY supplier_id DESC Commented May 19, 2016 at 10:04

1 Answer 1

4

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

Sign up to request clarification or add additional context in comments.

1 Comment

This is a better query than mine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.