【问题标题】:The query argument of wpdb::prepare() must have a placeholderwpdb::prepare() 的查询参数必须有一个占位符
【发布时间】:2016-06-02 06:08:57
【问题描述】:

我正在尝试更新一些旧代码以在 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(字符串?)还能用于数组还是不起作用?

【问题讨论】:

    标签: php mysql wordpress


    【解决方案1】:

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

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

    【讨论】:

    • 感谢您的帮助,我明白了。 %d = 数字和 %s = 字符串。
    【解决方案2】:

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

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

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

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

    例如(如果 $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 ) );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-11-21
      • 2012-12-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-05
      相关资源
      最近更新 更多