SQL查询失败,没有错误消息

时间:2017-12-14 13:43:27

标签: php mysql codeigniter

我使用CodeIgniter 3而我正在尝试从

更改SQL查询
  

SELECT id,来自collect_id ='ubb'

的文档中的shelfmark

  

从document_revisions中选择document_id,shelfmark        其中collection_id ='ubb'和最新='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):

  

对象(CI_DB_mysqli_result)#65(8){[“conn_id”] =>对象(mysqli的)#16   (19){[“affected_rows”] => int(160)[“client_info”] =>串(79)   “mysqlnd 5.0.12-dev - 20150407 - $ Id:   b5c5906d452ec590732a93b051f3827e02749b83 $“[”client_version“] =>   int(50012)[“connect_errno”] => int(0)[“connect_error”] =>空值   [ “错误号”] => int(0)[“error”] => string(0)“”[“error_list”] =>阵列(0)   {} [“field_count”] => int(2)[“host_info”] => string(25)“Localhost via   UNIX套接字“[”info“] => NULL [”insert_id“] => int(0)[”server_info“] =>   string(23)“5.7.18-0ubuntu0.16.04.1”[“server_version”] => INT(50718)   [ “STAT”] => string(139)“正常运行时间:252001主题:3个问题:36434   慢查询:0打开:501刷新表:1打开表:313查询   每秒平均值:0.144“[”sqlstate“] =>字符串(5)”00000“   [ “PROTOCOL_VERSION”] => int(10)[“thread_id”] => INT(2507)   [ “WARNING_COUNT”] => int(0)} [“result_id”] =>对象(mysqli_result)#38   (5){[“current_field”] => int(0)[“field_count”] => INT(2)   [ “长度”] => NULL [“num_rows”] => int(160)[“type”] => int(0)}   [ “result_array”] => array(0){} [“result_object”] =>数组(0){}   [ “custom_result_object”] => array(0){} [“current_row”] => INT(0)   [ “NUM_ROWS”] => NULL [“row_data”] => NULL}

affected_rows表示我在MySQL中直接运行SQL查询时获得的文档数量:[“affected_rows”] => INT(160)

1 个答案:

答案 0 :(得分:1)

如果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

相关问题