我没有获得Codeigniter网址。我当前的base_url是http://localhost/codeigniter/
我想从我的数据库中检索数据,但它总是告诉我
消息:未定义的变量:查询
或
消息:尝试获取非对象的属性
我确信我的控制器,模型和视图工作正常。我的数据库不是空的,但每次我输入此网址http://localhost/codeigniter/index.php/c_test/
时,它都不会显示结果/错误,我必须始终指定网址以查看我是否收到错误http://localhost/codeigniter/index.php/c_test/getgrades
(注意我必须添加getGrades以便我可以看到结果)
我也试过var_dump它,是的,它不是NULL。另一个问题是,它告诉我Message: Undefined variable: query
这是什么原因?
(我会告诉你代码,这样你就可以得到我的意思) 控制器:
function getGrades() {
$data['query'] = $this->m_test->result_getGrades();
$this->load->view('v_display', $data);
}
型号:
function result_getGrades()
{
$this->db->select('grades.blockcode,subjectblocking.subjectcode,subjects.description,grades.final');
$this->db->from('grades');
$this->db->join('subjectblocking','grades.blockcode=subjectblocking.blockcode');
$this->db->join('subjects','subjectblocking.subjectcode=subject.subjectcode');
$query->$this->db->get();
return $query->result();
}
查看:
<?php foreach ($query as $row): ?>
<?php echo $row->studentid;?><br>
<?php echo $row->subjectcode;?><br>
<?php echo $row->description;?><br>
<?php echo $row->final;?><br>
<?php endforeach; ?>
为什么查询未定义?我是否必须检查/更改config.php
和autoload.php
上的内容?
答案 0 :(得分:3)
尝试更换一行$ query-&gt; $ this-&gt; db-&gt; get(); to $ query = $ this-&gt; db-&gt; get();在您的模型函数中,
function result_getGrades()
{
$this->db->select('grades.blockcode,subjectblocking.subjectcode,subjects.description,grades.final');
$this->db->from('grades');
$this->db->join('subjectblocking','grades.blockcode=subjectblocking.blockcode');
$this->db->join('subjects','subjectblocking.subjectcode=subject.subjectcode');
$query = $this->db->get();
return $query->result();
}