Saul Saul - 1 year ago 81
SQL Question

Query in codeigniter

Im trying to select a field in table 'reasons' but i have to get the field 'reason' in the table 'customer_complaint' these tables are realted.

This is my model:

function reason(){
$reason = $this->db->select("*")->from('reason')->join('customer_complaint','customer_complaint.reason=reasons.reason_id')->get();
return $reason->results();

and im getting this error on the screen:

A Database Error Occurred

Error Number: 1146

Table 'complaint.reason' doesn't exist

"SELECT * FROM `reason` JOIN `customer_complaint` ON `customer_complaint`.`reason`=`reasons`.`reason_id`"

The query works fine in MySQL workbench but i think im getting this error because the framework are omitting 'customer_'.

Thank you guys in advance.


There are several errors I can see

First Table reason you mentioned used in Two form, 1. reason, 2. reasons

Make spelling correct first.

Second you use results() method which is wrong, it should result()

So If your table name is reasons then your query should be as following

 public function reason(){  
   $reason = $this->db->select('*')->from('reasons')->join('customer_complaint','customer_complaint.reason = reasons.reason_id')->get();
    return $reason->result();

and now try