【问题标题】:CodeIgniter MySQL Query not returning any data, even though there definitely is data to be returned!CodeIgniter MySQL Query 不返回任何数据,即使肯定有要返回的数据!
【发布时间】:2011-03-14 17:09:37
【问题描述】:

我有一个使用此功能和 ActiveRecord 设置的 CI 模型:

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 控制器中调用以下代码时: p>

    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 的错误日志。

【问题讨论】:

    标签: php mysql activerecord codeigniter


    【解决方案1】:

    首先,在控制器构造函数中启用分析器:

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

    这样您就可以准确地看到生成了哪些查询。

    LE:还有,别忘了$this-&gt;db-&gt;last_query(); ;)

    【讨论】:

    • 感谢您提醒我有关 Profiler 的信息。刚刚通过数据库运行它的查询 - 问题是,ActiveRecord 在 CURDATE() 周围加上“引号”,所以它被视为文本而不是 MySQL 函数!你知道有什么办法可以解决这个问题吗?
    • 知道了。 $this-&gt;db-&gt;where() 的第三个参数,当设置为 FALSE 时,不会添加反引号。谢谢博格丹!
    • 是的,CI 的 Active Record 转义机制有时真的是 PIA :)
    猜你喜欢
    • 1970-01-01
    • 2015-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-23
    • 2021-12-13
    • 2016-12-06
    相关资源
    最近更新 更多