【问题标题】:Codeigniter 2: Why does $this->db->get('documents') return false?Codeigniter 2:为什么 $this->db->get('documents') 返回 false?
【发布时间】:2017-06-13 14:58:47
【问题描述】:

我刚开始使用 CodeIgniter 2,可能我已经监督了一些明显的事情,但我无法弄清楚。

我的 MySQL 数据库有一个名为 Documents 的表,其中有一列名为 online_since。

函数 _getMaxOnlineSince() 应该获取最大的 online_since 值并将其作为字符串返回。

在 application/models/browse_model.php 中:

  private function _getMaxOnlineSince() {
    $this->db->select_max('online_since');
    $oQuery = $this->db->get('documents');
    return $oQuery->num_rows() > 0 ? $oQuery->row()->online_since : false;
  }

我在浏览器中收到一条错误消息:

致命错误:未捕获的错误:调用成员函数 num_rows() on 布尔值

因为 $oQuery 为 false:var_dump($oQuery) 返回 bool(false)

var_dump($this->db->select_max('online_since')) 返回对象(CI_DB_mysqli_driver)#14 (73) {...}

Codeigniter 2 应用程序在使用 PHP5.3 的旧服务器上工作,但在更新到 PHP 7 后,它在这一点上崩溃了。

为什么 $this->db->get('documents') 返回 false,我该如何解决?

我可以设法让这个函数与 sql 查询一起工作:

  private function _getMaxOnlineSince() {
    $oQuery = "select max(online_since) from documents";
    $sMaxOnlineSinceDate = $this->db->query($oQuery);
    return $sMaxOnlineSinceDate->num_rows() > 0 ? $sMaxOnlineSinceDate->row()->online_since : false;
  }

但我使用的地方还有很多

$this->db->get('documents')

我想就我而言,CodeIgniter 出于某种原因无法评估此语句。我会进一步调试。

【问题讨论】:

  • print_r($oQuery->result()) 返回什么?
  • 致命错误:未捕获错误:在布尔值上调用成员函数 result()
  • 表格是空的吗?
  • 表格中的文档为2000行。
  • 认为您需要找出 php 5.3 和 7.0 之间与 CI 相关的变化。检查系统文件,也许现在不推荐使用某些 CI 功能。您可能还想升级到 3.0,至少要检查它是否适用于新版本的 CI:codeigniter.com/user_guide/installation/upgrading.html

标签: php mysql codeigniter


【解决方案1】:

您可以通过以下方式修改您的函数_getMaxOnlineSince()

private function _getMaxOnlineSince() {
$this->db->select_max('id','online_since');
$res1 = $this->db->get('documents');
if ($res1->num_rows() > 0) {
    return $res1->row();
}
else {
    return false
}
return NULL;

}

另外请检查表名documents是否正确。希望对你有帮助

【讨论】:

  • 在您的示例中 $res1 仍然是错误的。表格文件存在。
  • 我在本地实现了上面的代码,取了两个字段 id 和 name 并插入了两个静态记录。当我打印 $res1->row();那么输出是 stdClass Object ( [name] => 2 )。所以请检查它是否对我有用
猜你喜欢
  • 2012-07-23
  • 2017-04-10
  • 1970-01-01
  • 2017-07-08
  • 2014-12-07
  • 2012-01-22
  • 2017-08-26
  • 2016-01-30
  • 1970-01-01
相关资源
最近更新 更多