我使用 PHP 和 MYSQL 数据库来检索我使用以下查询的一些数据:
$result2 = $wpdb->get_results('select siteID, siteNAME, equipmentTYPE from `site_info` where ownerID = 159');
foreach($result2 as $result) {
print_r ($result);
echo "<br/>";
但此查询返回以下格式:
stdClass Object ( [siteID] => BAH004 [siteNAME] =>XXXXXXX [equipmentTYPE] => XXXXXXX )
我希望格式如下: BAH004,XXXXXXX,XXXXXXX
谁能帮我解决这个问题吗?
最佳答案
根据wpdb::get_results ,您可以使用 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
所以,使用
$result2 = $wpdb->get_results('select siteID, siteNAME, equipmentTYPE from `site_info` where ownerID = 159', ARRAY_N);
应该可以解决问题。
<小时/>要获得所需的输出格式,不得使用 print_r
,因为 print_r
输出具有固定格式。而是自己做输出,例如
foreach($result2 as $result) {
echo join(', ', $result), '<br/>';
}
关于php - 修复尝试从数据库检索数据时 php 中的显示格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42206452/