0

I am trying to run a mysql query to SELECT rows based on the ID's provided, here is the query:

$result = $wpdb->get_results($wpdb->prepare("SELECT guid FROM bicp_posts WHERE ID IN (%s)", $gallery[0]));

The $gallery[0] variable contains a string as: "1440,1439,1417"

Problem: When I execute this query it gives me only one row in result instead of 3 rows, For debugging purpose I displayed the last executed query and it shows this executed query:

SELECT guid FROM bicp_posts WHERE ID IN ('1440,1439,1417');

Notice the commas inside IN brackets are the problem as the following query gives correct results: (without commas in brackets)

SELECT guid FROM bicp_posts WHERE ID IN (1440,1439,1417);

How can I bind the string in the query such that it is without the commas like the query written right above this sentence so that I can get correct results.

Mark
  • 121
  • 4
  • 13
  • 1
    The commas are not the problem, they are required. The problem is that you are passing a single string containing `'1440,1439,1417'` when you should be passing 3 distinct numbers `1440,1439,1417` – RiggsFolly Jun 29 '16 at 22:35
  • 1
    this would also work `SELECT guid FROM bicp_posts WHERE ID IN ('1440','1439','1417');` –  Jun 29 '16 at 22:37

0 Answers0