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 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行