1

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.

9
  • 1
    have you tried accessing the company_name property of $company_name? your getCompanyName method returns an object, not a string for a name Commented Nov 9, 2020 at 13:16
  • can you get $data['staff']; ?? Commented Nov 9, 2020 at 13:19
  • Which line is producing the error? Commented Nov 9, 2020 at 13:42
  • @lagbox yes I can access $company_name , as you can see ASGB is actually the company name Commented Nov 9, 2020 at 14:01
  • have you tried access the property of $company_name that is named company_name? $company_name->company_name Commented Nov 9, 2020 at 14:01

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

2 Comments

yes currect, jut needed to add. "$company_name->company_name;" thank you very much for the details.
@Roxana Slj please accept my answer if it solves your problem & future readers also.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.