类stdClass的对象无法转换为字符串CodeIgniter
我想根据用户名获取用户名,并将其插入到不同的表中。这是我的控制器:类stdClass的对象无法转换为字符串CodeIgniter,codeigniter,Codeigniter,我想根据用户名获取用户名,并将其插入到不同的表中。这是我的控制器: $customer = strtoupper($this->input->post('cust_name')); $address = $this->input->post('address'); $category = $this->input->post('category'); $sales = ($this->input->post('si
$customer = strtoupper($this->input->post('cust_name'));
$address = $this->input->post('address');
$category = $this->input->post('category');
$sales = ($this->input->post('sic'));
$salesID = $this->user_model->getUserName($sales);
$result = $this->db->insert('customer', [
'cust_name' => $customer,
'cust_address' => $address,
'category' => $category,
'user_id' => $salesID
]);
我的模型是:
public function getUserName($sales) {
$this->db->select("user_id");
$query = $this->db->get_where('user', ['name' => $sales]);
return $query->row();
}
当我执行查询时,它总是说“类stdClass的对象无法转换为字符串”
这里是var_转储:
对象(stdClass)#22(1){[“用户id”]=>string(2)“15”}
打印:
stdClass对象([user\u id]=>15)
如何解决这个问题?谢谢希望这对您有所帮助: 使用
行
对象如下:
$salesID = $this->user_model->getUserName($sales);
$result = $this->db->insert('customer', [
'cust_name' => $customer,
'cust_address' => $address,
'category' => $category,
'user_id' => $salesID->user_id /* here is the change*/
]);
替代方法:返回用户id
如下:
public function getUserName($sales) {
$this->db->select("user_id");
$query = $this->db->get_where('user', ['name' => $sales]);
return $query->row()->user_id;
}
/*then use it as you are using currently*/
尝试删除
$this->db->select(“用户id”)代码>行相同的stdClass错误,这里是打印结果stdClass对象([user\u id]=>15[name]=>admin[password]=>47840d48c90990005b6f6c332f1b667319fb592d0461f0e67931b20210e595b1[电子邮件]=>test@test.com[添加日期]=>0000-00-00:00:00[修改日期]=>0000-00-00:00:00)
尝试此操作以获取用户\u id$salesID->user\u id whoa。我用的是第一个,它正在工作。对我来说是新知识。谢谢你的好意,先生。我的荣幸,快乐