php - SQL 查询失败且没有错误消息

标签 php mysql codeigniter

我使用 CodeIgniter 3 并且我正在尝试更改 SQL 查询

SELECT id, shelfmark from documents where collection_id='ubb'

SELECT document_id, shelfmark from documents_revisions where collection_id='ubb' and latest='1'

第一个 SQL 查询是通过以下方式生成的:

 public function getDocumentsForCollection($sCollectionId) {
    $this->db->select('id, shelfmark');
    $this->db->where('collection_id', $sCollectionId);
    $query = $this->db->get('documents');
    if($query->num_rows() > 0) {
      return $query->result();
    }
    else {
      return '';
    }
  }    

为了匹配第二个 SQL 查询,我将此方法更新为

  public function getDocumentsForCollection($sCollectionId) {
    $this->db->select('document_id, shelfmark');
    $where = "collection_id=" . $sCollectionId . " AND latest=1";
    $this->db->where($where);
    $query = $this->db->get('documents_revisions');
    if($query->num_rows() > 0) {
      return $query->result();
    }
    else {
      return '';
    }
  }

当我打开执行该函数的页面时,我只看到一个空白页面。

如何在 CodeIgniter 中调试以查看 PHP 函数生成的 SQL 查询?

我更新的 PHP 函数出了什么问题?

<小时/>

我将方法更新为:

  public function getDocumentsForCollection($sCollectionId) {
    $this->db->select('document_id, shelfmark');
    $where = array("collection_id"=>$sCollectionId,"latest"=>1);
    $this->db->where($where);
    $query = $this->db->get('documents_revisions');
    var_dump($query); exit;
    if($query->num_rows() > 0) {
      return $query->result();
    }
    else {
      return 'foo';
    }
  }

在 if 条件之前使用 var_dump($query) 我得到:

object(CI_DB_mysqli_result)#65 (8) { ["conn_id"]=> object(mysqli)#16 (19) { ["affected_rows"]=> int(160) ["client_info"]=> string(79) "mysqlnd 5.0.12-dev - 20150407 - $Id: b5c5906d452ec590732a93b051f3827e02749b83 $" ["client_version"]=> int(50012) ["connect_errno"]=> int(0) ["connect_error"]=> NULL ["errno"]=> int(0) ["error"]=> string(0) "" ["error_list"]=> array(0) { } ["field_count"]=> int(2) ["host_info"]=> string(25) "Localhost via UNIX socket" ["info"]=> NULL ["insert_id"]=> int(0) ["server_info"]=> string(23) "5.7.18-0ubuntu0.16.04.1" ["server_version"]=> int(50718) ["stat"]=> string(139) "Uptime: 252001 Threads: 3 Questions: 36434 Slow queries: 0 Opens: 501 Flush tables: 1 Open tables: 313 Queries per second avg: 0.144" ["sqlstate"]=> string(5) "00000" ["protocol_version"]=> int(10) ["thread_id"]=> int(2507) ["warning_count"]=> int(0) } ["result_id"]=> object(mysqli_result)#38 (5) { ["current_field"]=> int(0) ["field_count"]=> int(2) ["lengths"]=> NULL ["num_rows"]=> int(160) ["type"]=> int(0) } ["result_array"]=> array(0) { } ["result_object"]=> array(0) { } ["custom_result_object"]=> array(0) { } ["current_row"]=> int(0) ["num_rows"]=> NULL ["row_data"]=> NULL }

affected_rows代表直接在MySQL中运行SQL查询得到的文档数量:["affected_rows"]=> int(160)

最佳答案

如果 db->where() 子句中有多个条件,则必须传递数组

$where = array("collection_id"=>$sCollectionId,"latest"=>1);

完整代码:

public function getDocumentsForCollection($sCollectionId) {
$this->db->select('document_id, shelfmark');
$where = array("collection_id"=>$sCollectionId,"latest"=>1);
$this->db->where($where);
$query = $this->db->get('documents_revisions');
if($query->num_rows() > 0) {
  return $query->result();
}
else {
  return 'foo';
}

}

将生成如下结果:

SELECT `document_id`, `shelfmark` FROM (`documents_revisions`) WHERE `collection_id` = 'ubb' AND `latest` = 1

使用Where子句完整指南:https://www.codeigniter.com/userguide3/database/query_builder.html#looking-for-specific-data

关于php - SQL 查询失败且没有错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47814813/

相关文章:

java - 使用java将html表单中的复选框数据插入到sql中

mysql - 初学者 MYSQL 错误 - 访问被拒绝

codeigniter - 在MVC中,处理和解析数据的应该是Model还是Controller?

php - 将发布日期与 Codeigniter 中的时间戳进行比较

PHP递归函数错误?

javascript - PHP Workerman - 客户端 websocket 连接问题

php - 在每三个 wordpress 帖子中添加一个类

javascript - Jquery 无法在 WordPress 中运行;函数 PHP

mysql - Sequelize : Performance advantage or just for convenience?

php - CodeIgniter - 仅当有查询要运行时才连接到数据库