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

标签 php mysql database wordpress echo

我使用 PHPMYSQL 数据库来检索我使用以下查询的一些数据:

$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/

相关文章:

python - 缺少索引约束添加外键

mysql - 用MYSQL查询多表的逻辑

android - 将 AutoCompleteTextView 与中等大小的字符串数据集一起使用的最佳方法

mysql - 引用第一个表到第二个表的多个行ID,以便每次都可以显示第一个表中的所有行

php - 如何在没有用户访问页面的情况下运行 php 代码?

php - 单击按钮运行 SQL 查询

php - Drupal 7 表单未显示在页面上

php - 将 php post 数据传递给 javascript 的方法

php - 通过比较日期时间查询以检查预订可用性

MySQL - 需要在单个字符串中查找多个通配符