2

I want to return some data from my database with Codeigniter. The query is a join from all related tables. Here is teh diagram: enter image description here

The join query from the model:

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();
    }

In the database, I have this record: enter image description here

And in-browser here are the var_dumps results SQL statement and returned data. The records that are not OK acquisition date, latest edit, and status.

enter image description here

6
  • Try this echo $this->db->last_query(); after $query statement Commented Jul 18, 2019 at 9:09
  • the first paragraph from the last image is the print_r($this->db->last_query()); Commented Jul 18, 2019 at 9:15
  • could you please give the sql dump, then we can identify Commented Jul 18, 2019 at 9:22
  • 3
    You have aquisition_date fields in workstations and phones tables, because phones table is joined later in the query, it's value overrides the first one, give different aliases to those fields. Commented Jul 18, 2019 at 9:30
  • please show the last sql dump. It could be null because of LEFT JOIN. Commented Jul 18, 2019 at 10:02

1 Answer 1

1

You make SELECT * - select all columns from all specified tables. And in your tables some fields have the same names ('acquisition_date', 'latest_edit', 'status' - there are in 'phones' and 'workstations'). Therefore, you have an unpredictable result. Perhaps, phpmyadmin and Codeigniter use different drivers. In any case, using just SELECT * is very unreliable.

There are 2 solutions:

  1. Instead of $this->db->select('*'); use $this->db->select('explicitly list all need fields', NULL);.

For example:

$this->db->select('w.hostname ..., w.acquisition_date AS ws_acquisition_date ..., o.occupation_id ..., p.acquisition_date AS ph_acquisition_date ...', NULL);

Or at least for the fields of the same name in the request to explicitly specify the name-aliases:

$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);

And in PHP, refer to these fields as:

$return->ws_acquisition_date
$return->ph_acquisition_date
$return->ws_status
...
  1. Or rename the fields so that the names do not repeat.

I recommend the first solution.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.