doulu6929 2017-02-07 10:07
浏览 42
已采纳

Codeigniter:尝试获取行的非对象属性()

The error:

enter image description here

here's my code: enter image description here

function get_modal_awal(){
     date_default_timezone_set('Asia/Jakarta');
     $sql = "SELECT modal_awal
             FROM trans_modal_awal 
           WHERE date_format(tanggal,'%Y-%m-%d') = '".date('Y-m-d')."'
           AND kode_kasir = ".$_SESSION['kode_user']."";
     return $this->db->query($sql)->row()->modal_awal; //the error goes here
 }

my view code like this:

<input readonly class="form-control" type="text" id="modal_awal"name="modal_awal" value="<?php echo $modal_awal;?>"></input>

my controller:

$data['modal_awal'] = $this->Modal_kasir_model->get_modal_awal();

What should I do?

  • 写回答

2条回答 默认 最新

  • doukou4066 2017-02-07 10:18
    关注

    You are assuming that your SQL always returns a row. If it doesn't, then there is no object to read. I would try to trap this:

    function get_modal_awal(){
        date_default_timezone_set('Asia/Jakarta');
        $sql = "SELECT modal_awal
                FROM trans_modal_awal 
             WHERE date_format(tanggal,'%Y-%m-%d') = '".date('Y-m-d')."'
             AND kode_kasir = ".$_SESSION['kode_user']."";
    
        //Get row
        $result = $this->db->query($sql);
        if($result){
            $row = $result->row();
            if($row){
                //return row if set
                return $row->modal_awal;
            }
        }
        //return nothing, and deal with it in the calling code
        return false;
    
    }
    

    It's also not a bad idea to wrap your SQL calls in a try ... catch block.

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

报告相同问题?

悬赏问题

  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)