global $wpdb, $bp;
$result = $wpdb->get_results("SELECT user_id FROM wp_bp_xprofile_data WHERE value = '$_POST[language]' OR value = '$_POST[budget]' OR value = '$_POST[style]'LIMIT 0 , 30");
print_r($result);
输出结果为:
Array ( [0] => stdClass Object ( [user_id] => 2 )
[1] => stdClass Object ( [user_id] => 2 )
[2] => stdClass Object ( [user_id] => 2 )
[3] => stdClass Object ( [user_id] => 4 ) )
我希望该值为逗号分隔字符串。
答案 0 :(得分:1)
get_results()
将返回一个对象。您可以简单地遍历对象并使用对象表示法访问id,如下所示:
$arr = array();
foreach ($result as $k => $id) {
$arr[] = $id->user_id;
}
$string = implode(',', $arr);
或者,您可以改为使用get_col()
:
$result = $wpdb->get_col("SELECT user_id
FROM wp_bp_xprofile_data
WHERE value = '$_POST[language]' OR value = '$_POST[budget]'
OR value = '$_POST[style]'LIMIT 0 , 300");
现在,要获取以逗号分隔的字符串,请使用implode()
:
$string = implode(',', $result);
答案 1 :(得分:0)
当你必须获得一列
时,使用wpdb的get_col$result = $wpdb->get_col("SELECT user_id FROM wp_bp_xprofile_data WHERE value = '$_POST[language]' OR value = '$_POST[budget]' OR value = '$_POST[style]'LIMIT 0 , 30");
echo implode(",",$result);
or
echo join(",",$result);
答案 2 :(得分:0)
hier是一种可能的解决方案;
<?php
global $wpdb, $bp;
$result = $wpdb->get_results("SELECT user_id FROM wp_bp_xprofile_data WHERE
value = '$_POST[language]' OR value = '$_POST[budget]'
OR value = '$_POST[style]'LIMIT 0 , 30");
$str_ids = '';
foreach($result as $r) {
$str_ids .= ('' == $str_ids) ? '' : ', ';
$str_ids .= $r->user_id;
}
print_r($str_ids);
答案 3 :(得分:0)
试试这个:
$str=implode(",",json_decode(json_encode($result),true));