我应该足够简单,我有我的查询,我将其作为变量返回,然后将该变量设置为数组,将其传递到视图中。 我怎么会得到这个错误..
Error Number: 1142
SELECT command denied to user '******** ip.secureserver.net' for table 'Comments'
SELECT * FROM `Report_Comments`.`Comments`, `Report_Comments`.`Comment_Date`, `Login`.`Username` WHERE `ReportID` = '53'
Filename: models/report/Report_model.php
Line Number: 92
有人看到我哪里出错了吗?
型
function get_comment()
{
$query = $this->db->get('Report_Comments.Comments, Report_Comments.Comment_Date, Login.Username')
->from('Report_Comments')
->join('Login', 'Report_Comments.UserID = Login.LoginID');
return $query->result();
}
视图
<?php if (isset($reports)) :
foreach ($reports as $row) : ?>
<tr>
<td><?php echo $row->Comments; ?></td>
</tr>
<tr>
<td><?php echo $row->Comment_Date; ?></td>
</tr>
<tr>
<td><?php echo $row->Username; ?></td>
</tr>
</table>
<hr>
<?php endforeach; ?>
<?php else : ?>
<p>No Comments</p>
<?php endif; ?>
控制器
function comments()
{
$data = array();
$this->db->where('ReportID', $this->uri->segment(3));
if ($query = $this->report_model->get_comment()) {
$data['reports'] = $query;
}
$this->template['middle'] = $this->load->view($this->middle = 'comments/comment_view', $data, true);
}
新答案:
Sql 告诉您指定的用户没有对表 Comments
运行 select 命令的权限。您需要检查您的用户是否具有数据库和/或表的适当权限,以解决该 mysql 问题。你的PHP不应该与此有任何关系。
问题/问题的原始答案:
(编辑让我失望了。
为了获取包含行的result
对象,您需要调用返回该对象的函数。
所以在你的控制器中
$data['reports'] = $result->result();
变量赋值用法
同样在您的模型中,设置$result = $this->db->get();
返回将只是传回$this->db->get();
- 删除$result =
是没有用的。
在你的控制器中,你正在测试if($result = $this->report_model->get_comment())
$this->report_model->get_comment()
的值,所以,如果这个值是一个get对象,那么php如何解释为真或假是有点松散的结束 - 它将返回它的"真实性",这并不总是直截了当的。或者,您可以执行某些明确操作,例如:
$query = $this->report_model->get_comment();
if ($query->num_rows() > 0) {
$data['reports'] = $result->result();
}
您也可以将该$query直接传递给视图,然后代替if (isset($reports))
=> if ($query->num_rows() > 0): foreach ($query->result() as $row):
执行该测试。如果检查,这将减少一个。
跨函数的数据库查询生成器
因此,当您通过各种级别的函数构建查询时,您最终会遇到更多的错误或奇怪的情况。此外,随着代码库的增长,维护代码库会更加困难,因为您以不可预测的方式使用模型函数。不要在调用模型函数之前设置 where,如果您希望 id 是可选的,请将 id 作为具有预定义值的参数传递:
function get_comment($report_id = null)
{
if (isset($report_id)) {
$this->db->where('ReportID', $report_id);
}
$this->db->select('Report_Comments.Comments, Report_Comments.Comment_Date, Login.Username')
->from('Report_Comments')
->join('Login', 'Report_Comments.UserID = Login.LoginID');
return $this->db->get();
}
而您的控制器:
function comments()
{
$data = array();
$query = $this->report_model->get_comment($this->uri->segment(3));
if($query->num_rows() > 0)
{
$data['reports'] = $query->result();
}
$this->template['middle'] = $this->load->view ($this->middle = 'comments/comment_view',$data, true);
$this->layout();
}