0

I have an array with following values:

$test = [2,8,10,12]; 

and a table 'orderdetails' in database with order_title and order_id columns.

What I want is to fetch order_title of every order using order_id in a loop. Can anyone help?

What I know is:

$test = [2,8,10,12];
foreach($test as $order_id){
$order_name = $wpdb->get_results('SELECT order_title FROM orderdetails WHERE order_id ='. $order_id);
$order_names[] = $order_name;
}

but this will make query run multiple times, any better solution?

UPDATE:

When I try to use IN(), values are getting inserted like this:

SELECT order_title FROM orderdetails WHERE order_id IN(2,8,10,12)

but what problem is here, query is only returning the value for first element in array that is 2

the solution needs to get applied is IN('2','8','10','12') all the values should be in a single quote, but I don't know how to achieve that.

5
  • Besides the fact that the given query is vulnerable to SQL injection: why not use IN()? Commented Feb 23, 2022 at 8:39
  • @NicoHaase I tried using IN() but the problem is the values needs to be in single invaded comma else it won't work. Commented Feb 23, 2022 at 8:41
  • Whatever you mean by "single invaded comma": please share your attempts such that others can see where this is going wrong Commented Feb 23, 2022 at 8:41
  • @NicoHaase updated in question. Commented Feb 23, 2022 at 8:52
  • Did you check whether get_results returns an array that you could iterate over? Commented Feb 23, 2022 at 9:00

2 Answers 2

1

You can use WHERE IN :

$ids = implode("','", [2,8,10,12]); 
$query = "SELECT order_title FROM orderdetails WHERE order_id IN ('$ids')";
$order_name = $wpdb->get_results($query);

Query :

SELECT order_title FROM orderdetails WHERE order_id IN ('2','8','10','12')
Sign up to request clarification or add additional context in comments.

2 Comments

thanks for quick answer, but problem with this solution is: query is only returning the value for first element in array that is 2 the solution needs to get applied is IN('2','8','10','12') all the values should be in a single quote, but I don't know how to achieve that.
@SanadQazi check my update
0

I'd do it like this:

$test = [2,8,10,12];
$inclause = '';
foreach($test as $order_id){
    $inclause .= $order_id . ',';
}
$inclause = rtrim($inclause,',');
$query = 'SELECT order_title FROM orderdetails WHERE order_id IN ('. $inclause . ')';
$order_name = $wpdb->get_results($query);

2 Comments

thanks for quick answer, but problem with this solution is: query is only returning the value for first element in array that is 2 the solution needs to get applied is IN('2','8','10','12') all the values should be in a single quote, but I don't know how to achieve that.
How about this then? 3v4l.org/ian7E

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.