CodeIgniter MySQL Query不返回任何数据,即使肯定有数据要返回!

时间:2010-07-12 09:06:09

标签: php mysql activerecord codeigniter

我使用此功能设置了CI模型和ActiveRecord:

function get_open_competitions()
{                                       
    $this->db->select('*, TO_DAYS(closingdate) - TO_DAYS(CURDATE()) AS days')
                ->from('challenges')
                ->where('closingdate >','CURDATE()')
                ->order_by('days','asc');

    $query = $this->db->get();          
    return $query;
}

我有99.9%的信心可以运行此查询:

SELECT *, TO_DAYS(closingdate) - TO_DAYS(CURDATE()) AS days
FROM challenges
WHERE closingdate > CURDATE()
ORDER BY days ASC

当我通过phpMyAdmin或Sequel Pro放置该简单查询时,它会从数据库返回5行 - 正如预期的那样。但是当我在challenges控制器中调用以下代码时:

    function index()
{
    // Fetch the Open for Entry competitions
    $data['open'] = $this->cm->get_open_competitions(); 
    // Fetch the Open for Voting competitions
    $data['voting'] = $this->cm->get_voting_competitions();
    // Fetch the Ended Competitions
    $data['ended'] = $this->cm->get_ended_competitions();   

    $data['colwide'] = 'challenges/challengeshome';
    $this->load->view('templatewide',$data);
}

...然后在视图文件中调用它......

<h2>Open for Entry</h2>

<hr/>

<?php foreach ($open->result() as $row) { ?>
    <h3>
        <?php echo anchor('challenges/view/'.$row->id, $row->title);?> - 
        <i>Challenge ends and voting begins in <?php echo $row->days;?> days</i>
    </h3>
    <h4> <?php echo $row->description;?> </h4>
<?php } ?>

......没有输出!

这让我感到困惑,因为我非常确定我有一个有效的查询,而且我还有另外两个模型函数 - get_ended_competitionsget_voting_competitions - 我正在使用它们除此之外哪个工作正常。代码肯定没有什么不同。

我做错了什么?! :S

谢谢!

杰克

编辑:没有任何内容写入CodeIgniter日志或PHP的错误日志。

1 个答案:

答案 0 :(得分:1)

在其他任何事情之前,在控制器构造函数中启用探查器:

$this->output->enable_profiler(true);

通过这种方式,您可以准确地看到生成的查询。

LE:另外,不要忘记$this->db->last_query();;)