Object of class stdClass could not be converted to string in php codeigniter

I am trying to get data from my database , based on the company_name but getting error:

Error:Object of class stdClass could not be converted to string 

staff controller :

function index()
{
  $user_id =  $this->session->userdata('manager');
    // echo "index"; die; 
   $company_name = $this->staff_model->getCompanyName($user_id);

  //  print_r($company_name); 
  //      die;
   $data['staff'] = $this->staff_model->getStaffDetails($company_name);  //Error on this line

// print_r($data); 
// die;
   $this->load->view('manager/staff/index',$data);
}

staff_model

 <?php
class Staff_model extends CI_Model
{
        //table name: user_login
        function getCompanyName($user_id)
        {
          $company_name=$this->db->select('company_name')->from('user_login')->where(array('id' => $user_id,'delete_flag'=>0))->get()->row();
          return $company_name;
        }
        function getStaffDetails($company_name)
        { 
         $delete_flag=0;
         return  $this->db->get_where('user_login',array('delete_flag!='=>$delete_flag , 'company_name'=>$company_name))->result();  //Error on this line 

         // $query = $this->db->select('*')
          //   ->from('user_login')
          //   ->where("(delete_flag = '$delete_flag )")
          //   ->where("(company_name = $company_name)"); //Error on this line
         //   return $query();  }

And running

 print_r($company_name); 
            die;

results:

stdClass Object ( [company_name] => ASGB )

Would appreciate if anyone can help.

1 answer

  • answered 2020-11-09 15:06 KUMAR

    Change in your Model of Codeigniter:-

    staff_model:-

    <?php
    class Staff_model extends CI_Model
    {
            //table name: user_login
            function getCompanyName($user_id)
            {
                $company_name = $this->db->select('company_name')
                   ->from('user_login')
                   ->where(array('id' => $user_id,'delete_flag'=>0))
                   ->get()->row();
                return $company_name->company_name;   //change this.
            }
            
    }
    

    Note:- This method returns a single result row. If your query has more than one row, it returns only the first row. The result is returned as an object.

    $row = $query->row();

    echo $row->column_name; // accessing variable of row(); function by ->(Arrow(->) operator:).

    Fore more reference regarding this check :-

    https://www.codeigniter.com/userguide3/database/results.html#result-rows