【问题标题】:get result of query step by step codeigniter逐步获取查询结果codeigniter
【发布时间】:2018-09-15 18:20:54
【问题描述】:

我需要这样做:

if (isset($account)) {
    $this->db->where('account', $account);
}

if (isset($subject)) {
    $this->db->where('subject', $subject);
}

if (isset($contact)) {
    $this->db->where('_from', $contact);
}
//here i need to get result
$resul1 = $this->db->get('table');

$limit = 5; $offset =10;
$this->db->limit($limit, $offset);
//here i need to get result with offset and limit
$result2 = $this->db->get('table');

$result1 应该执行这个查询

SELECT *
FROM `table`
WHERE `account` = 'some account'
AND `subject` = 'some subject'
AND `_from` = 'name'

并且 $result2 应该执行这个查询($query1 + 偏移量和限制):

SELECT *
FROM `table`
WHERE `account` = 'some account'
AND `subject` = 'some subject'
AND `_from` = 'name'
LIMIT 10, 5

$result2 作为单独的查询执行:select * FROM table LIMIT10, 5

是否有可能实现这一点,或者我需要从头开始查询?

【问题讨论】:

  • 如果一个查询似乎就足够了,我想知道您为什么要执行两个查询。
  • @Jamie_D 我需要确定最大页码。我正在开发一个 rest api 服务,所以我不能使用 CI 分页库。
  • @u_mulder 听起来这是不可能的(至少在 CI2 中)。

标签: php database codeigniter codeigniter-3


【解决方案1】:

LIMIT 没有limit()函数

result = $this->db->get('table', 0, $offset);

或者使用手动选择:

$query = "SELECT *, COUNT(account) AS total FROM `table` WHERE `account` = ? AND `subject` = ? AND `_from` = ? LIMIT 0, ?";
$result = $this->db->query($query, array($account, $subject, $contact, $offset));

【讨论】:

  • 这个查询只执行简单的选择,没有任何WHERE。必须考虑其他条件(帐户、主题、联系人)
  • 帐户、主题、联系人被替换为条件数组(在结果中显示)参见 $this->db->query() 和查询绑定 [link]bsourcecode.com/codeigniter/codeigniter-select-query
  • 这里有更好的文档来解释查询绑定。见本页底部:[link]codeigniter.com/userguide2/database/queries.html
  • 我认为没有办法完全按照我的意愿去做。我通过 rejax 解决了它(删除 LIMIT (digit) 以及它之后的所有内容。
猜你喜欢
  • 2021-11-30
  • 1970-01-01
  • 1970-01-01
  • 2012-07-06
  • 2021-07-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多