Welcome Guest, Not a member yet? Register   Sign In
Is this right -Select, Joins, Where in Codeigniter?
#1

[eluser]brucebat[/eluser]
Hi all,

I have been building my first join in codeigniter however I don't think I have done it correctly as the variable dump of the result query is giving me this:


Quote:object(CI_DB_mysql_result)#22 (8) { ["conn_id"]=> resource(30) of type (mysql link persistent) ["result_id"]=> resource(40) of type (mysql result) ["result_array"]=> array(0) { } ["result_object"]=> array(0) { } ["custom_result_object"]=> array(0) { } ["current_row"]=> int(0) ["num_rows"]=> int(0) ["row_data"]=> NULL }


Should it not be giving me an array of values from my tables I have selected and joined?


Here is my model code with a var_dump purely in there just to test my code.

Code:
public function view_record($record_id)
        {
            $criteria = array
            (
                'procedure_id' => $record_id
            );
            

            $this->db->select('procedure.procedure_id, procedure.patient_id, procedure.department_id, procedure.name_id , procedure.dosage_id');
            $this->db->from ('procedure');
            
            $this->db->join('patient', 'patient.patient_id = procedure.patient_id', 'inner');
            $this->db->join('department', 'department.department_id = procedure.department_id', 'inner');
            $this->db->join('procedure_name', 'procedure_name.procedure_name_id = procedure.name_id', 'inner');
            $this->db->join('dosage', 'dosage.dosage_id = procedure.dosage_id', 'inner');
            
            $this->db->where('procedure_id', $record_id);
            $result = $this->db->get();
            
            var_dump($result);
    
        
            return;


        }

Here is my Schema

http://i.imgur.com/Dju0G.png

Thanks for your time.

I was following the example in User Guide
#2

[eluser]brucebat[/eluser]
Okay update:

I added the following code:

Code:
if ($returned_record->num_rows() > 0)
            {
                echo "There is a record";
            }
            
            else
            {
                echo "No record";
            }

Just for testing and I am not getting "No record"
#3

[eluser]brucebat[/eluser]
Solved

Turns out it is better to use Left joins for rows that may not have foreign keys!
#4

[eluser]Aken[/eluser]
You also need to use some form of $result->result() to retrieve records. var_dump($result) acts as it should.




Theme © iAndrew 2016 - Forum software by © MyBB