Codeigniter 获取数据错误:尝试获取非对象的属性
Posted
技术标签:
【中文标题】Codeigniter 获取数据错误:尝试获取非对象的属性【英文标题】:Codeigniter fetching data error: Trying to get property of non-object 【发布时间】:2015-09-22 14:27:12 【问题描述】:我正在使用 CI 从数据库中获取数据。每当我输出结果时,我总是得到一个错误和一个空值。错误是:
A php Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: user/joinroomlanding.php
Line Number: 31
输出页面控制器:
public function joinroom()
$data['title'] = "User| Join Room";
$data['message'] = $this->session->flashdata('message');
$data['room'] = $this->user_model->joinRoom();
if($this->session->userdata('is_logged_in') == 2 )
$this->load->view('include/header_user',$data);
$this->load->view('include/navbar_user');
$this->load->view('user/joinroomlanding');
$this->load->view('include/footer_user');
else
redirect('restricted');
验证输入:
public function joinRoomValidation()
$this->load->library('form_validation');
$this->load->model('user_model');
$this->form_validation->set_rules('roomCode', 'Room Code', 'required|xss_clean|numeric');
if($this->form_validation->run() == true)
if($this->user_model->joinRoom() == true)
$this->session->set_flashdata('message', "<p class='alert alert-success'><span class='glyphicon glyphicon-ok' aria-hidden='true'></span> Room found successfully.</p>");
redirect('joinroomlanding');
else
$this->session->set_flashdata('message', "<p class='alert alert-danger'><span class='glyphicon glyphicon-exclamation-sign' aria-hidden='true'></span> You need to enter a valid room code.</p>");
redirect('user_roomlist');
else
$this->session->set_flashdata('message', "<p class='alert alert-danger'><span class='glyphicon glyphicon-exclamation-sign' aria-hidden='true'></span> You need to enter a room code.</p>");
redirect('user_roomlist');
型号:
public function joinRoom()
$roomCode = $this->input->post('roomCode');
$this->db->select('*');
$this->db->where('room_code', $roomCode);
$query = $this->db->get('room');
if($query->num_rows() == 1)
return $query->row_array();
else
return false;
joinroomlanding.php
<div class="text-center"><?php echo $message;?></div>
<div class="panel panel-green">
<div class="panel-body">
<div class="text-center">
<h3><?php echo $room->room_name; ?></h3>
<h3><?php var_dump($room->room_name); ?></h3>
<h3><?php echo $room->room_code; ?></h3>
<h3><?php echo $room->date_created; ?></h3>
</div>
</div>
</div>
</div>
原生 php 查询解决这个问题(joinroomlanding.php):
$classCode=$this->input->post('classCode');
$sql= "SELECT * FROM class WHERE class_code='".$classCode."'";
$result= mysql_query($sql);
while($row = mysql_fetch_array($result))
echo $row['class_name'];
echo $row['class_section'];
echo $row['class_description'];
echo $row['class_limit'];
【问题讨论】:
您能显示代码中第 31 行(来自错误)的位置吗? 【参考方案1】:我相信问题正在发生,因为您的模型使用 row_array()
返回结果,这意味着您的结果是数组格式,这意味着您需要像 $result['room_name']
一样使用 []
来获取它们,但是,当你在你的视图中迭代这些结果,你假设这些结果是对象形式的,因此这样做:<?php echo $room->room_name; ?> ...
为了解决这个问题,改变你的模型返回方法:
...
if($query->num_rows() == 1)
return $query->row_array();
else
...
到这里:
...
if($query->num_rows() == 1)
return $query->row();
else
...
来源:http://www.codeigniter.com/userguide3/database/results.html#result-rows
希望这会有所帮助。
更新:
要将其转换为 Codeigniter:
$classCode=$this->input->post('classCode');
$sql= "SELECT * FROM class WHERE class_code='".$classCode."'";
$result= mysql_query($sql);
while($row = mysql_fetch_array($result))
echo $row['class_name'];
echo $row['class_section'];
echo $row['class_description'];
echo $row['class_limit'];
执行以下操作:分解属于您的模型、控制器和视图的内容:
型号:
function model_method($classCode)
$this->db->select();
$this->db->from('class');
$this->db->where('class_code', $classCode);
$q = $this->db->get();
return $q->result_array();
控制器:
function controller_method()
$classCode = $this->input->post('classCode');
$this->load->model('model_name');
$data['result'] = $this->model_name->model_method($classCode);
$this->load->view('my_view', $data);
查看:
echo $result['class_name'];
echo $result['class_section'];
echo $result['class_description'];
echo $result['class_limit'];
【讨论】:
实际上已经有人提到答案return $query->row();
它返回一个布尔值false。
那么这意味着您得到 0 个结果或超过 1 个结果。您需要对结果运行测试,然后根据修复您的条件
我的朋友用这个查询解决了它我编辑它你能帮我把它转移到codeigniter吗?
是的,我可以帮助你。你到底需要什么?以上是关于Codeigniter 获取数据错误:尝试获取非对象的属性的主要内容,如果未能解决你的问题,请参考以下文章