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 关于#sql#的问题:#情况描述 在用vs对项目进行调试时,出现找不到网络路径,然后查看SQL配置工具,发现SQL服务显示远程调用过程失败(相关搜索:防火墙)
  • ¥15 主要进行描述泥浆在管路不同区段泥浆的密度不相同,泥浆的密度有高有低,此时管路的摩阻分布需要怎么计算,(标签-matlab)
  • ¥40 通过编制程序计算圆管内层流充分发展对流换热,参数如图5-4,以及公式5-16所示,要求用表5-6对程序计算准确性进行验证
  • ¥20 该学习哪个编程语言? AI会取代程序员吗?
  • ¥15 ensp如何拼通IP地址
  • ¥15 saber软件导入Ibis模型报错
  • ¥15 商城生产日期批次库存问题
  • ¥15 esp8266控制共阳极wrgb灯板无法关闭所有led灯
  • ¥100 python读取速度问题
  • ¥15 stm32f407使用DMA问题