doulu7921 2018-12-10 16:59
浏览 65

$ wpdb - > get_col返回重复的数组

I'm trying to get all the values for all sessions under the "session_value" column in the "wp_woocommerce_sessions" DB table.

The following code does the job but for some reason is duplicating the result 6 times, giving me 6 arrays with the exact same content.

global $wpdb;
$wooSessions = $wpdb->get_col( "SELECT session_value FROM {$wpdb->prefix}woocommerce_sessions" );
$wooCookies = implode(" ", $wooSessions);

I'm reading about OBJECT_K in the documentation which in theory discards duplicated keys but I don't know how to apply it. If I add OBJECT_K inside the get_col parenthesis the code stops working.

This is the output var_dump ($wooSessions); is giving me:

array(1) { [0]=> string(4366) "a:12:{s:4:"cart";s:2084:"a:2:{s:32:"1b4cfd194c804d1ad422a350aa2a0145…"}

array(1) { [0]=> string(4366) "a:12:{s:4:"cart";s:2084:"a:2:{s:32:"1b4cfd194c804d1ad422a350aa2a0145…"}

array(1) { [0]=> string(4366) "a:12:{s:4:"cart";s:2084:"a:2:{s:32:"1b4cfd194c804d1ad422a350aa2a0145…"}

array(1) { [0]=> string(4366) "a:12:{s:4:"cart";s:2084:"a:2:{s:32:"1b4cfd194c804d1ad422a350aa2a0145…"}

array(1) { [0]=> string(4366) "a:12:{s:4:"cart";s:2084:"a:2:{s:32:"1b4cfd194c804d1ad422a350aa2a0145…"}

array(1) { [0]=> string(4366) "a:12:{s:4:"cart";s:2084:"a:2:{s:32:"1b4cfd194c804d1ad422a350aa2a0145…"}

Here's another example using session_id and session_key:

global $wpdb;
$wooSessions = $wpdb->get_results( "SELECT DISTINCT session_id, session_key FROM {$wpdb->prefix}woocommerce_sessions GROUP BY session_key" );

Which returns the following:

Array ( [0] => stdClass Object ( [session_id] => 2149 [session_key] => 76f0552cff4af84e1963815c1fc4a14d ) ) 
Array ( [0] => stdClass Object ( [session_id] => 2149 [session_key] => 76f0552cff4af84e1963815c1fc4a14d ) ) 
Array ( [0] => stdClass Object ( [session_id] => 2149 [session_key] => 76f0552cff4af84e1963815c1fc4a14d ) ) 
Array ( [0] => stdClass Object ( [session_id] => 2149 [session_key] => 76f0552cff4af84e1963815c1fc4a14d ) ) 
Array ( [0] => stdClass Object ( [session_id] => 2149 [session_key] => 76f0552cff4af84e1963815c1fc4a14d ) ) 
Array ( [0] => stdClass Object ( [session_id] => 2149 [session_key] => 76f0552cff4af84e1963815c1fc4a14d ) )

Here's another example selecting session_id and group by session_id:

global $wpdb;
$wooSessions = $wpdb->get_results( "SELECT session_id FROM {$wpdb->prefix}woocommerce_sessions GROUP BY session_id" );

This is the result:

Array ( [0] => stdClass Object ( [session_id] => 2149 ) )
Array ( [0] => stdClass Object ( [session_id] => 2149 ) )
Array ( [0] => stdClass Object ( [session_id] => 2149 ) )
Array ( [0] => stdClass Object ( [session_id] => 2149 ) )
Array ( [0] => stdClass Object ( [session_id] => 2149 ) )
Array ( [0] => stdClass Object ( [session_id] => 2149 ) )

Any ideas that can point me in the right direction? Thanks in advance.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 BP神经网络控制倒立摆
    • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
    • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
    • ¥30 Unity接入微信SDK 无法开启摄像头
    • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
    • ¥20 cad图纸,chx-3六轴码垛机器人
    • ¥15 移动摄像头专网需要解vlan
    • ¥20 access多表提取相同字段数据并合并
    • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
    • ¥20 Java-Oj-桌布的计算