0
votes

I have a variable called $result which should store all the results from my database query however when I run print ($result) it gives an error message:

A PHP Error was encountered

Severity: 4096

Message: Object of class CI_DB_mysql_result could not be converted to string

Filename: models/control_panel_model.php

Line Number: 72

Here is my model code:

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

            echo $this->db->count_all('procedure');
            return;
            $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();


            print($result);


            return ;


        }

The reason I am using a print function is just to test if my query is working and it has values. How do I achieve this.

Thanks

EDIT!!!

Here is what I get in var_dump();

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
}

I HAVE ADDED THE FOLLOWING CODE:

if ($result->num_rows >0) { echo "Data"; } else { echo "No Data"; }

IT IS SAYING THERE IS "NO DATA" so my query must be crap and wrong , so I need to redesign query. THERE IS DATA IN MY DATABASE SO must be query

===========================================

Here is my schema http://i.imgur.com/Dju0G.png

enter image description here

4
Thanks however it is returning all this: 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 } Is this my query result? - sqlmole
@sqlmole: Please add the var_dump output to your question, not as a comment. Thanks. - hakre

4 Answers

1
votes

The object in question (CI_DB_mysql_result) is missing the __toString implementation that's why you get the error.

However, this can mean that the object can not be usefully converted to string.

Instead you should convert the objects data to string first on your own for the purpose of your print operation. I can not say from the code you've posted which use you're looking for specifically, so there is not more I can add to this.

Please see Generating Query Results and Active Record Class.

Edit: For a quick and dirty string-printing, you can use print_r, however I do not know if this is actually helpful for your case:

$result = $this->db->get();
print_r($result);

That's comparable to the suggestion given to use var_dump. You should say which kind of information you would like to print actually and for this you should know the type of data the database returns. See the documentation of get()Active Record Class for some examples how to work with the return values.

1
votes

Use the function result_array() that returns the query result as a pure array, or an empty array when no result is produced:

...
$result = $this->db->get()->result_array();
print($result);
...
0
votes

The get() method does not return a result. Try changing your query and $result-lines to something like this

$query = $this->db->get_where('procedure_id', $record_id);
$result = $this->db->result();
0
votes

num_rows is a method, not a property.