【问题标题】:MVC query join command deniedMVC 查询连接命令被拒绝
【发布时间】:2016-04-28 03:38:01
【问题描述】:

我应该很简单,我有我的查询,我将它作为一个变量返回,然后将该变量设置为一个数组,然后将它传递到视图中。我怎么会得到这个错误..

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);
}

【问题讨论】:

    标签: php sql codeigniter model-view-controller


    【解决方案1】:

    新答案:

    Sql 告诉您指定的用户没有权限在表Comments 上运行选择命令。您需要检查您的用户是否具有对 db 和/或表的适当权限以解决该 mysql 问题。您的 PHP 不应该与此有任何关系。

    原始问题/问题的原始答案:

    (编辑让我失望。)

    为了获得包含行的result 对象,您需要调用返回该对象的函数。

    所以在你的控制器中

    $data['reports'] = $result-&gt;result();

    变量赋值用法

    同样在您的模型中,设置$result = $this-&gt;db-&gt;get(); 是没有用的,返回只会传回$this-&gt;db-&gt;get(); - 删除$result =。

    在您的控制器中,您正在测试if($result = $this-&gt;report_model-&gt;get_comment()) 中$this-&gt;report_model-&gt;get_comment() 的值,因此,如果该值是一个get 对象,那么php 如何将其解释为true 或false 有点松散——它会返回它的“真实性”,这并不总是直截了当的。或者,你可以做一些明确的事情:

    $query = $this->report_model->get_comment();
    if ($query->num_rows() > 0) {
        $data['reports'] = $result->result();
    }
    

    您也可以直接将 $query 传递给视图,并代替您的 if (isset($reports)) => if ($query-&gt;num_rows() &gt; 0): foreach ($query-&gt;result() as $row): 进行该测试。如果检查,那将减少一个。

    跨功能的数据库查询构建器

    因此,当您通过不同级别的函数构建查询时,您最终会遇到更多错误或奇怪的情况。此外,随着代码库的增长,维护代码库变得更加困难,因为您以不可预测的方式使用模型函数。如果您希望它是可选的,而不是在调用模型函数之前设置 where ,而是将 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();
    }
    

    【讨论】:

    • 这是我有过的最有见地的答案之一,我从你那里学到了很多+1非常感谢你
    猜你喜欢
    • 2021-07-31
    • 1970-01-01
    • 2017-08-08
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    • 1970-01-01
    • 2018-04-30
    • 2018-08-20
    相关资源
    最近更新 更多