Question

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

The output is:

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

I want the value as comma separated string.

Was it helpful?

Solution 2

hier is a possible solution;

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

OTHER TIPS

get_results() will return an object. You can simply loop through the object and access the id using object notation, like so:

$arr = array();
foreach ($result as $k => $id) {
    $arr[] = $id->user_id;
}
$string = implode(',', $arr);

Alternatively, you can use get_col() instead:

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

And now, to get the comma-separated string, use implode():

$string = implode(',', $result);

Use get_col of wpdb when you have to get one column

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

Try this:

$str=implode(",",json_decode(json_encode($result),true));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top