有什么方法可以对数组implode('OR',$ myArray)使用wpdb prepare语句吗? [英] Is there any way to use wpdb prepare statements for array implode(' OR ', $myArray)?

查看:84
本文介绍了有什么方法可以对数组implode('OR',$ myArray)使用wpdb prepare语句吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为$ wpdb正确准备数据

I'm trying to properly prepare my data for $wpdb

$region = $wpdb->get_results( $wpdb->prepare( "
      SELECT tr.*, count(*) AS jobs_count FROM {$wpdb->terms} tr
      INNER JOIN {$wpdb->term_taxonomy} tm
        ON ( tm.term_id = tr.term_id )
      INNER JOIN {$wpdb->term_relationships} tmr
        ON ( tmr.term_taxonomy_id = tm.term_taxonomy_id )
      INNER JOIN {$wpdb->posts} p
        ON ( p.ID = tmr.object_id )
      WHERE (tm.parent = '%d'
        AND tm.taxonomy = '%s'
        AND p.post_type = '%s' )
      GROUP BY name HAVING COUNT(name) > '%d'
  ", 0, 'location', 'job', 0 ));

我尝试过此操作以获取区域名称.它正按预期返回数据.但是这里的区域是父分类法,而国家是它的子项.因此,我还想准备数据以获取国家/地区列表.但是这里的问题是它在父元素下查找(如果存在).因此,我为其创建了一个名为$ sql的动态数组. 我要准备的代码在下面以$ country_query给出.

I tried this for get the region name. It's exactly return the data as expected. But here region is the parent taxonomy and the country is it's child item. So I also wanted to prepare the data for getting the country list also. But here the problem is it's looks for under the parent element if it exists. So I make a dynamic array for that named it $sql. The code I want to prepare is given below as $country_query.

在WHERE语句中,我使用php implode方法进行动态查询构建. 现在对我有用.但是我也想像区域一样准备数据.

In WHERE statement I use php implode method for dynamic query building. It's works for me now. But I also want to prepare the data as like region.

foreach ($region as $reg) {
      $sql[] = " $wpdb->term_taxonomy.parent = '$reg->term_id' ";
}
    $country_query = "SELECT $wpdb->terms.*, count(*) AS jobs_count FROM $wpdb->terms
                      INNER JOIN $wpdb->term_taxonomy
                        ON ( $wpdb->term_taxonomy.term_id = $wpdb->terms.term_id )
                      INNER JOIN $wpdb->term_relationships
                        ON ( $wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id )
                      INNER JOIN $wpdb->posts
                        ON ( $wpdb->posts.ID = $wpdb->term_relationships.object_id )

                      WHERE (". implode(' OR ', $sql) ." AND $wpdb->term_taxonomy.taxonomy = 'location' ) AND $wpdb->posts.post_type = 'job'
                      GROUP BY name HAVING COUNT(name) > 0";

    $country = $wpdb->get_results($country_query);

当前,我的查询在使用implode('OR',$ sql)this之后抛出此SQL语句.如果有人知道该怎么做,将对您有所帮助.

Currently my query throw this SQL statement after using implode(' OR ', $sql) this. It'll be helpful if anyone knows how to do this please please let me know.

SELECT wp_terms.*, count(*) AS jobs_count FROM wp_terms
                  INNER JOIN wp_term_taxonomy
                    ON ( wp_term_taxonomy.term_id = wp_terms.term_id )
                  INNER JOIN wp_term_relationships
                    ON ( wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id )
                  INNER JOIN wp_posts
                    ON ( wp_posts.ID = wp_term_relationships.object_id )

                  WHERE ( wp_term_taxonomy.parent = '2'  OR  wp_term_taxonomy.parent = '322'  OR  wp_term_taxonomy.parent = '651'  AND wp_term_taxonomy.taxonomy = 'location' ) AND wp_posts.post_type = 'job'
                  GROUP BY name HAVING COUNT(name) > 0

我也在尽力找到答案.如果我能找到任何答案,我也会分享我的答案.但问题是我不知道如何准备implode('OR',$ sql).如果不可能,而且任何人都知道,那么我必须忽略这一点,或者尝试另一种方法.

I'm also try my best to find an answer. if I can find any answer I'll also share my answer. But the thing is I've no idea how to prepare implode(' OR ', $sql). If it's not possible and anyone knows then I've to ignore this or try an another approach.

预先感谢...

推荐答案

感谢 https://stackoverflow.com/users/1704961/mdma .他对我说请尝试.所以最后我找到了stackoverflow的答案: https://stackoverflow.com/a/10634225/1993427 很多. :)

Thanks to https://stackoverflow.com/users/1704961/mdma for the heads up. He says to me try IN. So finally I found an answer from stackoverflow: https://stackoverflow.com/a/10634225/1993427 It helps me a lot. :)

因此,下面为其他人提供了我的最终代码.

So my final code is given below for others.

$country = array();

  $sql = array();
  foreach ($region as $reg) {
    $sql[] = $reg->term_id;
  }

$test = array("location", "job", "0");


$sql_st = "SELECT tr.*, count(*) AS jobs_count FROM {$wpdb->terms} tr
                  INNER JOIN {$wpdb->term_taxonomy} tm
                    ON ( tm.term_id = tr.term_id )
                  INNER JOIN {$wpdb->term_relationships} trm
                    ON ( trm.term_taxonomy_id = tm.term_taxonomy_id )
                  INNER JOIN {$wpdb->posts} p
                    ON ( p.ID = trm.object_id )

                  WHERE tm.parent IN(".implode(', ', array_fill(0, count($sql), '%s')).") AND tm.taxonomy = '%s' AND p.post_type = '%s'
                  GROUP BY name HAVING COUNT(name) > '%s'";

$country_query = call_user_func_array(array($wpdb, 'prepare'), array_merge(array($sql_st), $sql, $test));

$country = $wpdb->get_results($country_query);

干杯:)

这篇关于有什么方法可以对数组implode('OR',$ myArray)使用wpdb prepare语句吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆