dpkajqd31574096 2017-02-13 14:28
浏览 65
已采纳

尝试从数据库中检索数据时,在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?

  • 写回答

1条回答 默认 最新

  • dourao1968 2017-02-13 14:32
    关注

    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/>';
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵