Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Codeigniter从数据库中返回Null一个非Null的字段_Php_Mysql_Codeigniter - Fatal编程技术网

Php Codeigniter从数据库中返回Null一个非Null的字段

Php Codeigniter从数据库中返回Null一个非Null的字段,php,mysql,codeigniter,Php,Mysql,Codeigniter,我想用Codeigniter从数据库返回一些数据。该查询是来自所有相关表的联接。 以下是图表: 来自模型的联接查询: public function combine_all_data_related($hostname){ $this->db->select('*'); $this->db->from('workstations w'); $this->db->join('occupations o', 'w.

我想用Codeigniter从数据库返回一些数据。该查询是来自所有相关表的联接。 以下是图表:

来自模型的联接查询:

public function combine_all_data_related($hostname){
        $this->db->select('*');
        $this->db->from('workstations w');
        $this->db->join('occupations o', 'w.occupation_id = o.occupation_id', 'left');
        $this->db->join('departments d', 'd.department_id = o.department_id', 'left');
        $this->db->join('workstation_configuration wc', 'wc.service_tag = w.service_tag', 'left');
        $this->db->join('workstation_models wm', 'wm.workstation_model_id = wc.workstation_model_id', 'left');
        $this->db->join('workstation_types wt', 'wt.workstation_type_id = wm.workstation_type_id', 'left');
        $this->db->join('users u', 'u.occupation_id = o.occupation_id', 'left');
        $this->db->join('phone_numbers pn', 'pn.fmid = u.fmid', 'left');
        $this->db->join('phones p', 'p.phone_number_id = pn.phone_number_id', 'left');
        $this->db->join('phone_brands pb', 'pb.phone_brand_id = p.phone_brand_id', 'left');
        $this->db->where("w.hostname = '" . $hostname . "'");

        $query = $this->db->get();

        return $return = $query->result();
    }
在数据库中,我有以下记录:

在浏览器中,这里是var_dumps results SQL语句和返回的数据。不正常的记录获取日期、最新编辑和状态


您使
选择*
-从所有指定的表中选择所有列。在您的表格中,有些字段具有相同的名称(“采集日期”、“最新编辑”、“状态”-“电话”和“工作站”中都有)。因此,你有一个不可预测的结果。也许,phpmyadmin和Codeigniter使用不同的驱动程序。无论如何,仅使用
SELECT*
是非常不可靠的

有两种解决方案:

  • 而不是
    $this->db->select('*')使用
    $this->db->select('显式列出所有需要的字段',空)
  • 例如:

    $this->db->select('w.hostname ..., w.acquisition_date AS ws_acquisition_date ..., o.occupation_id ..., p.acquisition_date AS ph_acquisition_date ...', NULL);
    
    或者至少对于请求中相同名称的字段,显式指定名称别名:

    $this->db->select('*, w.acquisition_date AS ws_acquisition_date, p.acquisition_date AS ph_acquisition_date, w.status AS ws_status, w.latest_edit AS ws_latest_edit, p.latest_edit AS ph_latest_edit', NULL);
    
    在PHP中,将这些字段称为:

    $return->ws_acquisition_date
    $return->ph_acquisition_date
    $return->ws_status
    ...
    
  • 或者重命名字段,使名称不重复

  • 我推荐第一种解决方案。

    试试这个echo$this->db->last_query();$query语句之后,最后一幅图像的第一段是print\r($this->db->last\u query());请给出sql转储,然后我们可以确定工作站和电话表中是否有“获取日期”字段,因为电话表稍后将在查询中加入,它的值将覆盖第一个,为这些字段提供不同的别名。请显示最后一个sql转储。由于左联接,它可能为null。