You are using LEFT/RIGHT
joins so when there is no association found between your tables a null row will be returned ,second thing is your tables new_invoice
and executive_client_unit_relation
both have common names for the column name booking_id
and in your select statement you are doing select *
so for the columns with same name i guess the last one is picked which is null,for the solution you either you select only needed columns not all like SELECT cb.*
or either the columns have same name with your query table then specify them individually with the alias you wish to provide but in the end of select statement in query like SELECT *,cb.id AS booking_id,....
Codeigniter not showing proper results for array fetched by MySQL query.
-
21-07-2023 - |
Question
I am working with Codeigniter with mysql databases, I have a model function
public function get_all_data(){
$this->output->enable_profiler(TRUE);
$years = $this->input->post('year');
$months = $this->input->post('month');
$exec_ids = $this->input->post('exec_id');
$query = $this->db->query("SELECT * FROM client_booking AS cb
RIGHT OUTER JOIN executive_client_unit_relation AS ecur ON cb.id = ecur.booking_id
LEFT OUTER JOIN new_invoice ON cb.id = new_invoice.booking_id
WHERE ecur.executive_id IN (" . implode(',', $exec_ids) . ") AND MONTH(cb.booking_date) IN (" . implode(',', $months) . ") AND YEAR(cb.booking_date) IN (" . implode(',', $years) . ")");
return $query->result();
}
The sql generated by this is (as seen in the profiler) :
SELECT * FROM client_booking AS cb
RIGHT OUTER JOIN executive_client_unit_relation AS ecur ON cb.id = ecur.booking_id
LEFT OUTER JOIN new_invoice ON cb.id = new_invoice.booking_id
WHERE ecur.executive_id IN (4,5,6) AND MONTH(cb.booking_date) IN (1,2,3,4,5) AND YEAR(cb.booking_date) IN (2013,2014)
My problem is that the booking_id
value is set to NULL
when I var_dump()
the result of this query.
On the other hand, when I run the SQL in my phpmyadmin, everything goes well and I can see the booking_id
PS: client_booking
table's primary key is id
which is the booking_id
for all the other tables.
Can anyone help me to resolve this?
La solution
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow