尝试从数据库中检索数据时,在php中修复显示格式

i am using PHP with MYSQL database in order to retrieve some data i used the below query :

$result2 = $wpdb->get_results('select siteID, siteNAME, equipmentTYPE from `site_info`  where ownerID = 159');
foreach($result2 as $result) {
    print_r ($result);
    echo "<br/>";

but this query return this format :

stdClass Object ( [siteID] => BAH004 [siteNAME] =>XXXXXXX [equipmentTYPE] => XXXXXXX ) 

where i want the format to be like this : BAH004, XXXXXXX, XXXXXXX

can anyone help me to fix this problem?

According to wpdb::get_results, you can use ARRAY_N

wpdb::get_results( string $query = null, string $output = OBJECT )
$output

(string) (Optional) Any of ARRAY_A | ARRAY_N | OBJECT | OBJECT_K constants. With one of the first three, return an array of rows indexed from 0 by SQL result row number. Each row is an associative array (column => value, ...), a numerically indexed array (0 => value, ...), or an object. ( ->column = value ), respectively. With OBJECT_K, return an associative array of row objects keyed by the value of each row's first column's value. Duplicate keys are discarded.

Default value: OBJECT

So, using

$result2 = $wpdb->get_results('select siteID, siteNAME, equipmentTYPE from `site_info`  where ownerID = 159', ARRAY_N);

should do the trick.


To have the desired output format, you must not use print_r, because print_r output has a fixed format. Rather do the output yourself, e.g.

foreach($result2 as $result) {
    echo join(', ', $result), '<br/>';
}