Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm trying to run a series of wordpress database queries using each of the values in a php array, then store the query results in a separate php array. Here's the code I'm using, but it doesn't work and I can't figure out why. Would appreciate your advice.

<?php

    $query_values = array(7,13,27);

    $query_results = array();

    foreach ($query_values as $key => $value) {
        $this_result = $wpdb->get_results("SELECT my_column1 FROM wp_my_table WHERE my_column2=$value");
        array_push($query_results, $this_result);
    }

?>
share|improve this question

closed as off-topic by Anonymous, andrewsi, Hanky 웃 Panky, josilber, Ruchira Gayan Ranaweera Jul 21 at 4:00

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – Anonymous, andrewsi, Hanky 웃 Panky, josilber, Ruchira Gayan Ranaweera
If this question can be reworded to fit the rules in the help center, please edit the question.

1  
What doesn't work? Is $query_results an empty array? Does the query result in an error? –  Dave Chen Jul 20 at 23:35

1 Answer 1

up vote 1 down vote accepted

"It doesn't work and I can't figure out why" is kind of vague. What have you tested so far? Where did you stop figuring out? What did you figure out?

I'm not familiar with WordPress so much, but if above is all your code then I think it won't work because no database connection is being made. But I guess that is not your problem, since I guess this is only a snippet.

So what you would do then is:

  • Figure out if there is any error message. Is your PHP error logging on and if so, is it being printed to the screen (you could enable that if not) or is it being logged to a logfile?
  • If your errorlogging is OK and there is no errormessage than you'd have to figure it out yourself by debugging:
  • What happens if you run a static query to the database? For example: first run a query in PHPMyAdmin or any other tool you use and make sure the query works. Then you run that same query from your code and see if that works too. If not: you know the query is OK, so it must be your database connection, or the way you use the database wrapper.
  • If a static query works fine, die() the query you're using in the loop above. Then copy that query and run it from your databasetool to see if it works there.
  • Etc.

That's how you debug. Can you continue from there?

share|improve this answer
    
Apologies for being vague, and thanks for your help. –  user1684046 Jul 21 at 0:18

Not the answer you're looking for? Browse other questions tagged or ask your own question.