down123321123 2016-03-04 10:01
浏览 19
已采纳

使用Codeigniter检索查询结果

I use pivot to select some record(s) from my table. This is the query:

$data['master'] = $this->db->query("select * from 
        (
         select row, c.Nilai,b.Fullname,a.KodePenilaian,d.Description from penilaian_header a 
        left join employee b on a.Nip = b.Nip 
        left join outlet d on a.Outlet = d.OutletCode
        left join (select ROW_NUMBER() OVER(PARTITION BY KodePenilaianH ORDER BY idPenilaiand DESC) AS Row, Nilai,KodePenilaianH from penilaian_Detail
        ) c on a.KodePenilaian = c.KodePenilaianH where a.Outlet like '%$outlet%' and Periode like '%$periode%'
        ) nilai 
        pivot
        (
        sum(nilai)
        for row in ([1],[2],[3],[4],[5])
        ) piv;")->result();

And this the result when I run it via MS SQL Management Tools

enter image description here

now, my problem is to retrieve these [1],[2],[3],[4],[5] values. When I do this:

echo "<pre>";print_r($data['master']);
die();

I only get this:

Array
(
    [0] => stdClass Object
        (
            [Fullname] => maya kristiana
            [KodePenilaian] => PE0000001
            [Description] => KIOS ATRIUM3
        )

    [1] => stdClass Object
        (
            [Fullname] => SHELVIA GITA PUTRI
            [KodePenilaian] => PE0000002
            [Description] => KIOS ATRIUM3
        )

)

Update: I found the solution, thanks to @Alex Tartan (see accepted answer below):

I simply changed my ->result() to ->result_array();

and the result is now as desired:

enter image description here

  • 写回答

2条回答 默认 最新

  • donglu2761 2016-03-04 10:16
    关注

    You might want to use

    ->result_array(); // instead of `->result();`
    

    Numerical properties for objects are "weird" to say the least.
    Plus, there's the hassle of reading them. You'd have to echo $obj->{1}
    Also, setting them implies the same approach: $obj->{1} = 'stuff'

    Codeigniter might not do that.

    Getting them as arrays will bypass this issue (hopefully)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料