PHP Codeigniter 数据库助手类 "No tables used"

标签 php mysql database codeigniter orm

好吧,这显然不是完整的代码,但这是错误所在的代码。

$this->db->select('post_likes');
$this->db->from('user_dash_posts');
$this->db->where('post_idn', $idn);
$this->db->where('isactive', '0');
if($this->db->count_all_results() > 0)
{
    $dataDB = $this->db->get();

关于我在此处显示的最后一行的 get() 语句,我得到“未使用表格”。我对整个 CI 数据库类/帮助器还是有点陌生​​,因为我以前更喜欢手写完整的查询器。但是我正在尝试在 ORM 层上自学并更好地熟悉它们。然而,这是我的问题的一个哑点。我试图弄清楚我在这个声明中做错了什么,解决方法是什么,如果可能的话,解释我是如何做错的,这样我就可以理解并且不会再犯了。

最佳答案

根据documentation :

Normally, when an Active Record call is completed, all stored information is reset for the next call.

如果您想重用查询,请使用同一页面底部的缓存方法。完成后不要忘记刷新缓存:

$this->db->start_cache();
$this->db->select('post_likes');
$this->db->from('user_dash_posts');
$this->db->where('post_idn', $idn);
$this->db->where('isactive', '0');
$this->db->stop_cache();

if($this->db->count_all_results() > 0) {
     $dataDB = $this->db->get();
}
$this->db->flush_cache();

编辑:

使用单个查询而不是两个查询的另一个选项:

$this->db->select('post_likes');
$this->db->from('user_dash_posts');
$this->db->where('post_idn', $idn);
$this->db->where('isactive', '0');
$query = $this->db->get();

if($query->num_rows() > 0) {
     $dataDB = $query->result();
}

关于PHP Codeigniter 数据库助手类 "No tables used",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12763387/

相关文章:

php - 如何使用 Zend Layout 作为 Wordpress 主题?

php - 通过 URL 缓存是一种好习惯吗?

php - 使用 PHP 和 MySQL 处理时间戳(我的理论正确吗?)

sql - oracle查询余额

java - 将数据从 java 程序存储到 MySQL 的最简单方法是什么?

php - 如何从 PHP 执行交互式命令?

php - 使用正则表达式从 php 中的字符串中提取文本

php - 如果新值在数组中,如何批量更新数据库? PHP

mysql - 将查询结果放入新表中

database - 我的 GridDB 节点不想加入同一个集群