【问题标题】:invalid argument supplied for foreach () for code "DateTime::createFromFormat"?为代码“DateTime::createFromFormat”的 foreach () 提供的参数无效?
【发布时间】:2019-08-01 03:42:30
【问题描述】:

我在codeigniter中设置了时间戳在数据库中,格式为(yyyy-mm-dd hh: ii: ss) to (yyyy-mm-dd)


    function login($patient_ext_id,$birth_dttm) {
            $this->db->select('xocp_ehr_patient.patient_ext_id, xocp_persons.person_nm, xocp_persons.birthplace, xocp_persons.birth_dttm, xocp_persons.status_cd, xocp_persons.race_nm');
            $this->db->from('xocp_ehr_patient');
            $this->db->join('xocp_persons', 'xocp_ehr_patient.person_id = xocp_persons.person_id');
            /*$this->db->join("xocp_persons","xocp_persons.person_id = xocp_ehr_patient.patient_ext_id","left");*/
            $this->db->where('xocp_ehr_patient.patient_ext_id', $patient_ext_id);
            $tgl = $this->db->select(DateTime::createFromFormat('Y-m-d','xocp_persons.birth_dttm'));
            $this->db->where($tgl, $birth_dttm);
            /*$query =  $this->db->get('xocp_persons');*/
            $query =  $this->db->get('');
            return $query->num_rows();
        }

文件数据库/DB_query_build.php 中的代码无效

public function select($select = '*', $escape = NULL)
    {
        if (is_string($select))
        {
            $select = explode(',', $select);
        }

        // If the escape value was not set, we will base it on the global setting
        is_bool($escape) OR $escape = $this->_protect_identifiers;

        foreach ($select as $val)
        {
            $val = trim($val);

            if ($val !== '')
            {
                $this->qb_select[] = $val;
                $this->qb_no_escape[] = $escape;

                if ($this->qb_caching === TRUE)
                {
                    $this->qb_cache_select[] = $val;
                    $this->qb_cache_exists[] = 'select';
                    $this->qb_cache_no_escape[] = $escape;
                }
            }
        }

        return $this;
    }

严重性:警告消息:为 foreach() 提供的参数无效 文件名:数据库/DB_query_builder.php

行号:294

【问题讨论】:

  • 上面的代码中没有foreach()
  • 是的,我必须做什么?
  • 你从哪里得到invalid argument supplied for foreach ()
  • 在文件database/DB_query_builder.php中,代码为:link
  • @RizkyDwiananto 你有一个奇怪的选择查询,你希望$tgl 有什么值?

标签: php sql codeigniter


【解决方案1】:

打印 $select。有没有获取数据数组

    if(is_array($select)){
 foreach ($select as $val)
        {
     $val = trim($val);
      if ($val !== '')
        {
        $this->qb_select[] = $val;
        $this->qb_no_escape[] = $escape;
      if ($this->qb_caching === TRUE)
     {
        $this->qb_cache_select[] = $val;
        $this->qb_cache_exists[] = 'select';
        $this->qb_cache_no_escape[] = $escape;
            }
           }
          }
}

【讨论】:

  • 我试过但“遇到 PHP 错误” 严重性:警告消息:非法偏移类型文件名:database/DB_query_builder.php 行号:667
【解决方案2】:

您的查询错误。您可以将登录功能替换如下。

function login($patient_ext_id,$birth_dttm) {
    $this->db->select('xocp_ehr_patient.patient_ext_id, xocp_persons.person_nm, xocp_persons.birthplace, xocp_persons.birth_dttm, xocp_persons.status_cd, xocp_persons.race_nm');
    $this->db->from('xocp_ehr_patient');
    $this->db->join('xocp_persons', 'xocp_ehr_patient.person_id = xocp_persons.person_id');
    /*$this->db->join("xocp_persons","xocp_persons.person_id = xocp_ehr_patient.patient_ext_id","left");*/
    $this->db->where('xocp_ehr_patient.patient_ext_id', $patient_ext_id);
    $this->db->having("DATE_FORMAT( xocp_persons.birth_dttm, '%Y-%m-%d') = '$birth_dttm'", "",false);
    /*$query =  $this->db->get('xocp_persons');*/
    $query =  $this->db->get('');
    return $query->num_rows();
}

【讨论】:

  • 这对我有用,谢谢@MohammedShafeek,所以查询“有”你能解释一下吗?
  • @Rizky Dwiananto 聚合部分将在没有 where 子句中的子句中起作用...但是您的错误是您在 mysql 查询列中集成了 PHP 函数...这是不可能的。
猜你喜欢
  • 2011-05-17
  • 2011-02-07
相关资源
最近更新 更多