我在 CodeIgniter 和实践中非常新鲜。我现在正在开发一个简单的 Codeigniter 应用程序,只是为了练习。我在数据库中有银行及其分支机构。我只想显示带有银行名称的分支机构。分支机构正在显示,但在获取银行时在 Controller 中显示此错误。我在 SO links 中尝试了这些, 但没有发现有效。
"A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object"
in Line Number: 85 and 91
这是我的 Controller
function ShowBranchList() {
$branch_list = $this->FD_Model->get_all_branches();
$get_bank = $this->FD_Model->get_bank_by_branch($branch_list->bankid); //This is Line Number 85
if($branch_list ){
$data = array(
'pagetitle' => 'Branch List View',
'branch_list_data' => $branch_list,
'br_bank' => $get_bank['bank_name'],//This is Line Number 93
);
}
型号
/*function for getting all branches*/
function get_all_branches() {
$this->db->order_by( $this->brid, $this->order );
return $this->db->get( $this->brtable )->result();
}
/*function for getting banks by branches*/
function get_bank_by_branch( $id ) {
$this->db->where( $this->bid, $id);
return $this->db->get( $this->banktable )->row();
}
最后,这是我的 View
foreach ($branch_list_data as $branch_list)
{
<?php echo $branch_list->brname ?>
<?php echo $br_bank; ?>
}
有人知道这个问题吗?
更新
当我想查看查询时
echo $this->db->last_query();
结果
SELECT * FROM
tbl_bankmasterWHEREbidIS NULL
值 $branch_list->bankid 是 null。当我硬编码一些值时, View 页面显示没有任何错误。
最佳答案
这样做
$get_bank = $this->FD_Model->get_bank_by_branch($branch_list[0]->bankid);
和
'br_bank' => $get_bank[0]->bank_name # this will work maybe
或
'br_bank' => $get_bank[0]['bank_name']
Its
0indexed array so you need to add the key to access the child's
关于php - 如何解决消息 : Trying to get property of non-object error in Codeigniter?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49270939/