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条)

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向