尝试从数据库中获取列值时获取非对象错误的属性

尝试从数据库中获取列值时获取非对象错误的属性

问题描述:

Hi I am trying to fetch the column value from the database my preview section so that each content is fetched on web page via database. I have matched my one table id to the the another table id and get the entire row as a result and save it in a variable and try to select a selective value from that row to show on my web page. But when i try to fetch the value of selective column the notice error was appeared on my page saying " Trying to get property of non-object". Please help me in this. Here is my codes:-

My view page code is:-

<ul>
    <li><p> <?= $seller->Name; ?></p></li>
    <li><p> <?= $switch->return_policy; ?> Return Policy</p></li>
    <li><p>Replacement  <?= $switch->replacement; ?> </p></li>
    <li><p>Cash On Delivery <?= $switch->cod; ?></p></li>       
</ul>    

The error shows in this line <li><p> <?= $seller->Name; ?></p></li>

My controller code was:-

public function Preview_Switch($switch_id)
   {
    if($this->session->userdata('seller_username') == "" && $this- 
               >session->userdata('seller_password') == ""){
        return redirect('seller/index');
    }
    else
    {
        $result = $this->sm->Preview_Switch($switch_id);
        $seller_id = $result->seller_id;
        $seller = $this->sm->Get_Seller_Info($seller_id);
        if($result)
        {
             $this->load->view('seller/Work_List/Switch_Preview', 
             ['switch'=>$result, 'seller'=>$seller]);
        }
    }
}

And my Model code is:-

enter code here
public function Get_Seller_Info($seller_id)
{
$get_seller_info = $this->db->get_where('seller',['Id'=>$seller_id]);
if($get_seller_info->num_rows > 0)
    {
        return $get_seller_info->row();
    }
    else
    {
        return false;
    }
}

The error shows only in seller table while I can get a value fro the switch table.

Change :

if($get_seller_info->num_rows > 0)

to :

if($get_seller_info->num_rows() > 0)

Reference :
query->num_rows versus query->num_rows()
codeigniter issue with num_rows (possible bug?)

If you use return $get_seller_info->row(); without specifying a row number, you'll not get what you expect.

If you're certain your query will return only one row, change that to return $get_seller_info->row(0); and you'll be able to use the code in your controller and view without further changes.

If you're not sure how many rows will be returned, change it to return $get_seller_info->result(); and then you can either iterate in your controller or view to walk through each returned row (regardless of how many those are) or access the data in a specific row by changing your view to <?= $seller[0]->Name; ?>