简体   繁体   English

CodeIgniter SQL查询不返回任何内容

[英]CodeIgniter SQL query returns nothing

I'm working with CodeIgniter and having the following problem. 我正在使用CodeIgniter,遇到以下问题。

My controller has: 我的控制器有:

class Site extends CI_Controller{
    function index()
    {
        $this->load->model('myModel');
        $data['records'] = $this->myModel->getAll();
        $this->load->view('home', $data);
    }
}

And my model has: 我的模型有:

class myModel extends CI_Model {
    function getAll()
    {
        $query = $this->db->get("test");
        return $query->result();
    }
} 

Finally, my view contains: 最后,我的观点包含:

<?php 
print_r($records);
foreach ($records as $item): ?>
    <li><?php echo $item->$title?></li>
<?php endforeach; ?>

The result is that I don't see my table as I should. 结果是我没有看到应有的表格。 Instead it prints out Array() . 而是打印出Array() $records has nothing inside it, and I believe it all begins in the controller. $records里面什么也没有,我相信一切都始于控制器。

Any suggestion would be of great help! 任何建议都会有很大帮助!

There is absolutely no problem with your code. 您的代码绝对没有问题。 You simply get an empty database table. 您只是得到一个空的数据库表。

Check the database table named test. 检查名为test的数据库表。 If it's empty, put some field values inside it. 如果为空,则在其中放入一些字段值。

You can even use: echo $this->db->last_query(); die; 您甚至可以使用: echo $this->db->last_query(); die; echo $this->db->last_query(); die; after your call to the model in the controller to see what query you are running. 在控制器中调用模型以查看正在运行的查询之后。 I'm sure it is ok. 我确定可以。

Try to display the log message. 尝试显示日志消息。 Maybe you'll get something. 也许你会得到一些。

Try this in your controller: 在您的控制器中尝试以下操作:

log_message('info', print_r($data,true));

And then check the log file. 然后检查日志文件。

好,谢谢大家的帮助,也很抱歉给您带来麻烦。我设法通过安装更新​​版本的xampp和php来找到解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM