简体   繁体   English

如何从PHP函数中返回MySQL字符串?

[英]How to return a MySQL string from within a PHP function?

I want to reuse the same MySQL query, so thought wrapping it in PHP function might help. 我想重用相同的MySQL查询,因此认为将其包装在PHP函数中可能会有所帮助。 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 . 但是事情不起作用,它返回null How do I make it work? 我该如何运作? What is wrong in the code? 代码有什么问题?

First, this is a great resource Class Reference $wpdb . 首先,这是一个很棒的资源类参考$ wpdb The , is making you $criteria the object type being return, not the search criteria; ,是让您$criteria返回的对象类型,而不是搜索条件; therefore, %s is empty. 因此, %s为空。 Add $criteria to the query string. $criteria添加到查询字符串。 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."'");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM