0

我尝试查询模型,但它给出了错误。如果我将查询放在控制器中,它工作正常。这是我的控制器(Home.php):

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Home extends CI_Controller {

    function __construct()
    {
        parent::__construct();
        $this->load->helper('form');
        $this->load->helper('asset');
        $this->load->database();
        $this->load->library('table');
    }

    public function index()
    {   
        $this->load->model('News');
        $resultNews = $this->News->queryNews();
        $data['resultNews'] = $resultNews;
        $this->load->view('front/index',$data);

    }
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */

这是我的模型(News.php):

<?php
if( !defined('BASEPATH')) exit('No direct script access allowed');
class News extends CI_Model {

    function __construct(){
        parent::__construct();
        $this->load->database();
    }

    function queryNews(){
        $this->db->query("SELECT * FROM  `news` WHERE news_show_date >= CURDATE() ORDER BY news_show_date DESC LIMIT 0 , 2");
        $query = $this->db->get();
        return  $query;

    }   
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */
?>

然后它吐出一个错误:

A Database Error Occurred
Error Number: 1096
No tables used
SELECT *
Filename: C:\AppServ\www\hijet\system\database\DB_driver.php
Line Number: 330

我是否在做错事而我错过了它?

4

1 回答 1

3

$this->db->query足够的。你应该删除$query = $this->db->get(); 所以代码将变为

    function queryNews(){
    $query = $this->db->query("SELECT * FROM  `news` WHERE news_show_date >= CURDATE() ORDER BY news_show_date DESC LIMIT 0 , 2");
    return  $query;

}

编辑:如果你想得到你应该使用$query->result()返回对象数组或$query->result_array()返回数组的结果。更多信息在这里 - http://ellislab.com/codeigniter/user-guide/database/results.html

于 2013-01-19T19:13:44.427 回答