Php (Codeigniter)为什么会出现此错误';正在尝试获取非对象的属性';
你能帮我做这个吗 我的控制器中没有任何代码,因为我直接从视图连接到模型 我的模型Php (Codeigniter)为什么会出现此错误';正在尝试获取非对象的属性';,php,codeigniter,Php,Codeigniter,你能帮我做这个吗 我的控制器中没有任何代码,因为我直接从视图连接到模型 我的模型 public function getSeller($id){ $row = $this->db->select('id_u, name_u, group_u') ->from('u_users') ->where('id_u', $id) ->where('
public function getSeller($id){
$row = $this->db->select('id_u, name_u, group_u')
->from('u_users')
->where('id_u', $id)
->where('group_u', 2)
->limit(1)
->get()
->row();
return $row->name_u;
}
我的看法
<td><? echo $this->generals->getSeller($gQ['id_s']); ?></td>
好吧,让我们输入一些调试代码,看看发生了什么,并记录下来。。。并添加修复程序 你的问题是,对于某些id,你不会得到你期望的结果!因此,您需要考虑导致空值的情况。我们怎么知道这个。。。因为我们解决了 这是我使用的代码。如果对此有任何问题,请提问
// Test conditions - Table entries
// We have an entry for id_u = 1, which has a group_u of 2
// We have an entry for id_u = 2, which has a group_u of 1
public function getSeller($id){
$row = $this->db->select('id_u, name_u, group_u')
->from('u_users')
->where('id_u', $id)
->where('group_u', 2)
->limit(1)
->get()
->row();
// Added next line for Debug
var_dump($row); // What actually happens - Let's See!
// From the var_dump we will get...
// null if no match was found - for $id = 2
//
// OR ( as seen using var_dump ) - for $id = 1
//
// object(stdClass)[19]
// public 'id_u' => string '1' (length=1)
// public 'name_u' => string 'Fred' (length=4)
// public 'group_u' => string '2' (length=1)
// Did we actually get a result?
if($row) {
// Yep, so $row->name_u should exist
return $row->name_u;
} else {
// Nope, so either return a string or a Boolean like FALSE or NULL
return "Nothing Found";
}
}
技术上
if($row) {
应该是
if($row !== NULL) {
这是对等价性和类型的测试。但这是另一天的讨论。我真的不知道在没有控制器的情况下如何加载视图。请走正确的路。在您的模型中,执行以下操作:
public function get_attendance_by_id($attendance_id)
{
$query = $this->db->select('*')
->from('attendance')
->where('attendance_id', $attendance_id)
->get();
return $query->row();
}
在控制器中:
public function __construct()
{
parent::__construct();
$this->load->model('Attendance_Model');
}
public function attendance_home($attendance_id)
{
$data['attendance'] = $this->Attendance_Model->get_attendance_by_id($attendance_id);
$this->load->view('attendance', $data);
}
在你看来:
<td><?= $attendance->username ?></td>
请看一下您确定吗?如果控制器中没有代码,那么如何调用视图?确实如此。除非CI在一夜之间神奇地改变,否则在没有控制器的情况下无法渲染视图