0

I want to reuse the same MySQL query, so thought wrapping it in PHP function might help. I did it as follows:

function fetch_from_db($criteria) {
    global $wpdb;
    $qvar = $wpdb->get_results("select * from $wpdb->terms, $wpdb->term_taxonomy where $wpdb->terms.term_id = $wpdb->term_taxonomy.term_taxonomy_id and $wpdb->term_taxonomy.taxonomy = %s", $criteria);
    return $qvar;
}
$get_two_wheeler_make = fetch_from_db('2-wheeler-make');

but things are not working and it is returning null. How do I make it work? What is wrong in the code?

7
  • What happens if you change "select * from $wpdb->terms... into something like: "select * from ".$wpdb->terms.".......? Commented Apr 28, 2016 at 14:07
  • @ThatMSG I get array(0) {} Commented Apr 28, 2016 at 14:14
  • @ThatMSG $wpdb->get_results("select * from" . $wpdb->terms, $wpdb->term_taxonomy . "where" . $wpdb->terms.term_id . "=" . $wpdb->term_taxonomy.term_taxonomy_id and $wpdb->term_taxonomy.taxonomy . "=" . $criteria); Commented Apr 28, 2016 at 14:15
  • Dont forget to keep the spaces: $wpdb->get_results("select * from " . $wpdb->terms.", ".$wpdb->term_taxonomy . " where " . $wpdb->terms.term_id . " = " . $wpdb->term_taxonomy.".".term_taxonomy_id." and ".$wpdb->term_taxonomy.taxonomy . " = %s" , $criteria); Commented Apr 28, 2016 at 14:18
  • @ThatMSG and does not need double quotes. I geave spaces everywhere else. Now I am getting result back but it is fetching all the result in the table. It doesnt consider $criteria at all that is passed to the function Commented Apr 28, 2016 at 14:24

1 Answer 1

1

First, this is a great resource Class Reference $wpdb. The , is making you $criteria the object type being return, not the search criteria; therefore, %s is empty. Add $criteria to the query string. And try again.

$qvar = $wpdb->get_results("SELECT * FROM $wpdb->terms, $wpdb->term_taxonomy
   WHERE $wpdb->terms.term_id = $wpdb->term_taxonomy.term_taxonomy_id
   AND $wpdb->term_taxonomy.taxonomy = '".$criteria."'");
Sign up to request clarification or add additional context in comments.

6 Comments

I just inserted plain text instead of $criteria, everything inside quotes, it works perfectly, but the variable is not working
following is working fine $qvar = $wpdb->get_results("select * from $wpdb->terms, $wpdb->term_taxonomy where $wpdb->terms.term_id = $wpdb->term_taxonomy.term_taxonomy_id and $wpdb->term_taxonomy.taxonomy = '2-wheeler-make'"); but, instead of that 2-wheeler-make I put $criteria and pass the variable from function then it doesnt work. Otherwise it works even without any hacks
Thank you very much. That last one worked! Yes it should have been handled like a string. Will mark this as correct answer!
for security should I do something about $criteria or does WP_Query handles it?
It is always safe to assume the worst. I would look into the wpdb class and see how it is handled. I know get_results() is passed to wpdb query. I would look there.
|

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.