Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Codeigniter获取数据错误:尝试获取非对象的属性_Php_Mysql_Database_Codeigniter_Phpmyadmin - Fatal编程技术网

Php Codeigniter获取数据错误:尝试获取非对象的属性

Php Codeigniter获取数据错误:尝试获取非对象的属性,php,mysql,database,codeigniter,phpmyadmin,Php,Mysql,Database,Codeigniter,Phpmyadmin,我正在使用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'] = "Us

我正在使用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');
    }
}
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>

我认为问题的出现是因为您的模型使用以下方法返回结果:
row\u array()
,这意味着您的结果是数组格式的,这意味着您需要使用
[]
$result['room\u name']
这样的方法来获取结果,但是,当您在视图中迭代这些结果时,您假设这些结果是对象形式的,因此执行此操作:

要修复此问题,请从以下位置更改模型返回方法:

...
if($query->num_rows() == 1){
        return $query->row_array();
} else {
...
为此:

...
if($query->num_rows() == 1){
        return $query->row();
} else {
...
资料来源:

希望这有帮助


更新:

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'];
要将其转换为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'];
控制器:

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'];
查看:

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'];

你能显示代码中第31行(来自错误)的位置吗?实际上有人已经提到了答案
return$query->row()它返回一个布尔值false。那么这意味着您要么得到0个结果,要么得到1个以上的结果。你需要对你的结果运行测试,然后根据它们修复你的条件我的朋友用这个查询解决它我会编辑它你能帮我把它转移到codeigniter吗?是的,我能帮你。你到底需要什么?