有没有办法使用wpdb prepare语句进行数组内爆('OR',$ myArray)?

时间:2015-03-11 23:29:39

标签: php mysql wordpress prepared-statement wpdb

我正在尝试为$ 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。

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

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)后抛出此SQL语句。如果有人知道该怎么做请帮助我,请告诉我。

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

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

提前致谢...

1 个答案:

答案 0 :(得分:2)

感谢https://stackoverflow.com/users/1704961/mdma提醒。他对我说试试IN。最后我从stackoverflow找到了答案:https://stackoverflow.com/a/10634225/1993427这对我有很大帮助。 :)

所以我的最终代码在下面给出了其他代码。

$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);

干杯:)