1

Odd one this. I have been working on this particular query for a few days now. The idea is to use the variables selected from first query to be used in second.

 ($project_id = $_GET['project_id'])|| ($project_id=$_POST['project_id']); $clients=$wpdb->get_results( " SELECT * FROM vo_wp_assigned_projects where project='$project_id'"); foreach($clients as $list){ $project=$list ->project; $client=$list ->client;//echo $client; $id=$list->client_id; echo $id; $name=$list->name; $users=$wpdb->get_results($wpdb->prepare( " SELECT * FROM vo_wp_clients where 'user_id'=%d", array( '$id'))); 

As a guide the echo $id; shows the variables but in the second query the 'where' element doesnt work, with all contents of table being displayed. I have looked at the wdp.prepare() on the Wordpress reference site. Any suggestions would be very helpful.

7
  • Are you trying to this as a batch, i.e. all $ids at once, or just one? At first glance your problem is you've got the string '$id' when you just want the value $id. I'm also not sure you need the array() around $id but it shouldn't matter. Commented Jan 28 at 15:44
  • A batch - I have done it in other queries. In the Wordpress guide they use inverted commas around the query. if not there the query doesnt work at all Commented Jan 28 at 15:54
  • Oh OK - on second glance that looks like it's still inside your loop though? To batch you should be collecting an array of IDs and then do a user_id in [] outside the loop, although unhelpfully it looks like wpdb prepare() doesn't really support that. And actually you also don't want the inverted commas around user_id either in the SQL string: if you want to quote a column name in MySQL that's backticks not inverted commas, but you don't need to here. Commented Jan 28 at 16:01
  • I did try that in my original query which wasnt a prepare - that didnt work. Have also used a foreach in and out of loop, nothing happens Also - the query shows nothing if the inverted commas are removed I'm sure its a simple fix. Ive been working on it so long that the wood is being hidden by the trees Commented Jan 28 at 16:16
  • Rup is right, '$id' is not a variable, it's a string. E.g. the program $id = 'test'; echo '$id'; prints $id not test. It must be $id without the quotes. Also how are you testing that it does or doesn't work? Can you fix the indenting/formatting? Commented Jan 28 at 18:06

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.