doujing1967 2015-11-26 07:51
浏览 38
已采纳

CodeIgniter return尝试获取非对象的属性

I've made an application in CodeIgniter this working in locally and now I want to try in on a hosting: hostinger. If you want see, check this. Now the problem's that when the I perform the installation of the library I get the error on the bottom from the code below:

public function get_admin_role_id()
{
    $result = intval($this->db->get_where('ea_roles', array('slug' => DB_SLUG_ADMIN))->row()->id);
    print_r($result);
    return $result;
}

A PHP Error was encountered

Severity: Notice

Message: Trying to get property of non-object

Filename: models/admins_model.php

Line Number: 372

Now this code should be returned the id roles of admin that is setted to default to 1. Now the installation is also involved in creating the structure of the tables and logging in phpMyAdmin seems that the database is still empty, perhaps this could be the cause of the problem? Can someone help me understand what's going on?

Complete admin model

  • 写回答

3条回答

  • doulao3078 2015-11-26 08:04
    关注

    It is not a good practice to use like this. We cannot ensure that the query will always produce a result it depends on database table. if $this->db->get_where returns no results then it cannot invoke the row() method and access the field idso it will raise an error.

    So always do like this

    public function get_admin_role_id()
    {
       $result = $this->db->get_where('ea_roles', array('slug' => DB_SLUG_ADMIN));
       if($result->num_rows()==0){
         echo "Result not found!";
       } 
       else{
        $row = $result->row();
        $id = intval($row->id);
      }
    }
    

    Please make sure that this query have some result.

    select * from ea_roles where slug='your_slug'; 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?