class stdClass 的对象无法在 php codeigniter 中转换为字符串
Object of class stdClass could not be converted to string in php codeigniter
我正在尝试根据 company_name 从我的数据库中获取数据,但出现错误:
Error:Object of class stdClass could not be converted to string
员工控制员:
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(); }
和运行
print_r($company_name);
die;
结果:
stdClass Object ( [company_name] => ASGB )
如果有人能提供帮助,我们将不胜感激。
更改您的 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.
}
}
注意:-此方法returns单个结果行。如果您的查询有多行,它 returns 只有第一行。结果以 object
.
形式返回
$row = $query->row();
echo $row->column_name; // 访问 row() 的变量; ->
(箭头(->) 运算符:).
函数
有关此检查的更多参考:-
https://www.codeigniter.com/userguide3/database/results.html#result-rows
我正在尝试根据 company_name 从我的数据库中获取数据,但出现错误:
Error:Object of class stdClass could not be converted to string
员工控制员:
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(); }
和运行
print_r($company_name);
die;
结果:
stdClass Object ( [company_name] => ASGB )
如果有人能提供帮助,我们将不胜感激。
更改您的 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.
}
}
注意:-此方法returns单个结果行。如果您的查询有多行,它 returns 只有第一行。结果以 object
.
$row = $query->row();
echo $row->column_name; // 访问 row() 的变量; ->
(箭头(->) 运算符:).
有关此检查的更多参考:-
https://www.codeigniter.com/userguide3/database/results.html#result-rows