简体   繁体   中英

CodeIgniter SQL query returns nothing

I'm working with CodeIgniter and having the following problem.

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() . $records has nothing inside it, and I believe it all begins in the controller.

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. 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; 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来找到解决方案。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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