wpdb :: prepare()的查询参数必须有占位符

时间:2016-06-02 06:08:57

标签: php mysql wordpress

我试图更新一些旧代码以在Wordpress插件中使用。

它正在做的是接受客户订单并检查他们之前是否已经购买了Woocommerce产品。如果有,它将获得最后一个购买日期。它实际上工作正常,但查询已过时。我想更新它以使用Wordpress 4.5。

我目前收到警告信息(3次):

wpdb :: prepare()的查询参数必须有占位符

$orders = get_posts( array(
        'meta_key'    => '_customer_user',
            'meta_value'  => get_current_user_id(),
            'post_type'   => 'shop_order',
            'post_status' => array( 'wc-processing', 'wc-completed' )
    ) );

$orders_id=wp_list_pluck( $orders, 'ID' ); //List of all order IDs

$product_id = $post->ID; // Get current product ID

$order_list='('.join(',', $orders_id).')';

global $wpdb;

$query_select_order_items = "SELECT order_item_id as id FROM {$wpdb->prefix}woocommerce_order_items WHERE order_id IN {$order_list}";

$query_select_product_ids = "SELECT meta_value as product_id FROM {$wpdb->prefix}woocommerce_order_itemmeta WHERE meta_key=%s AND order_item_id IN ( $query_select_order_items )";

$query_single_order_ids = "SELECT order_item_id FROM {$wpdb->prefix}woocommerce_order_itemmeta WHERE meta_key=%s AND meta_value=$product_id";

$products = $wpdb->get_col( $wpdb->prepare ( $query_select_product_ids,'_product_id' ) );

$order_item_id = $wpdb->get_col( $wpdb->prepare ( $query_single_order_ids, '_product_id' ) );

$hg_abb_li1 = $wpdb->get_col( $wpdb->prepare ( $query_select_order_items, '_product_id' ) );

$hg_abb_list1 = '('.join(',', $hg_abb_li1).')';
$hg_abb_list2 = '('.join(',', $order_item_id).')';

$query_select_hgabb_items = "SELECT order_id FROM {$wpdb->prefix}woocommerce_order_items WHERE order_item_id IN {$hg_abb_list1} AND order_item_id IN {$hg_abb_list2}";
$hg_abb_orderid = $wpdb->get_col( $wpdb->prepare ( $query_select_hgabb_items, '_order_item_id' ) );

$hg_abb_dateorderid = end($hg_abb_orderid);

$query_select_hgabb_date = "SELECT post_date FROM {$wpdb->prefix}posts WHERE ID = {$hg_abb_dateorderid}";
$l_order_date=$wpdb->get_col( $wpdb->prepare ( $query_select_hgabb_date, '_order_date' ) );

$last_order_date = (($l_order_date[0]));
编辑:那应该是这样吗?

$products = $wpdb->get_col( $wpdb->prepare ( "SELECT meta_value as product_id FROM ".$wpdb->prefix."woocommerce_order_itemmeta WHERE meta_key='_product_id' AND order_item_id IN ( SELECT order_item_id as id FROM ".$wpdb->prefix."woocommerce_order_items WHERE order_id IN (%s) )",$order_list ) );

$hg_abb_orderid = $wpdb->get_col( $wpdb->prepare ( 'SELECT order_id FROM '.$wpdb->prefix.'woocommerce_order_items WHERE order_item_id IN %s AND order_item_id IN %s', $hg_abb_list1, $hg_abb_list2 ) );

%s(字符串?)是否仍然可以用于数组或者不会起作用?

2 个答案:

答案 0 :(得分:3)

使用$wpdb->prepare时,必须将变量传递给查询。例如

    $min_id = 5
    $status = 'active'
    $wpdb->prepare( 
"SELECT id FROM wp_posts WHERE id > %d AND `post_status` = %s", $min_id, $status 
)

答案 1 :(得分:0)

钉了它。在$ wpdb-> prepare语句中对数组感到困惑。似乎您需要为每个数组元素生成占位符。

如果其他人需要帮助来创建占位符数组......

  • 计算数组
  • 使用array_fill生成%s(如果是字符串)或%d(如果是数字)
  • 内爆

这将创建一个%d,%d,%d,%d等字符串(以匹配数组中的值的数量),然后您可以将prepare语句与这些占位符和整个数组一起使用。< / p>

例如(如果$ orders_id是数组):

$orders_id_placeholders = implode( ', ', array_fill( 0, count( $orders_id ), '%d' ) );
$hg_abb_li1 = $wpdb->get_col( $wpdb->prepare ( "SELECT order_item_id as id FROM {$wpdb->prefix}woocommerce_order_items WHERE order_id IN ( $orders_id_placeholders )", $orders_id ) );