CodeIgniter Forums
Object of class CI_DB_mysql_result could not be converted to string - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Object of class CI_DB_mysql_result could not be converted to string (/thread-31172.html)



Object of class CI_DB_mysql_result could not be converted to string - El Forum - 06-09-2010

[eluser]Craig Ward[/eluser]
Hi,

I am getting the following error and I am not sure how to solve it

------------
A PHP Error was encountered

Severity: 4096

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

Filename: models/ManageProperty_model.php

Line Number: 21
------------

the model is

------------
<?php

class ManageProperty_model extends Model
{

function get_property_details($p_id, &$b_id)
{
$this->db->where('ID', $p_id);
$q = $this->db->get('properties');
$b_id = $this->db->query('SELECT BRANCH_ID FROM properties WHERE ID = "$p_id"');

if($q->num_rows() >0)
{
foreach ($q->result() as $row)
{
$data[] = $row;
}
return $data;
}
}

function get_property_images($p_id)
{
$this->db->where('property_id', $p_id);
$q = $this->db->get('media_image');

if($q->num_rows() >0)
{
foreach ($q->result() as $row)
{
$data[] = $row;
}
return $data;
}
}

}


Object of class CI_DB_mysql_result could not be converted to string - El Forum - 06-09-2010

[eluser]steelaz[/eluser]
So where is the line 21?

And what is the purpose of this line?
Code:
$b_id = $this->db->query('SELECT BRANCH_ID FROM properties WHERE ID = "$p_id"');



Object of class CI_DB_mysql_result could not be converted to string - El Forum - 06-09-2010

[eluser]Craig Ward[/eluser]
$b_id is what I am trying to get and pass back to the controller, that line is the one throwing the error


Object of class CI_DB_mysql_result could not be converted to string - El Forum - 06-09-2010

[eluser]mddd[/eluser]
You are passing &$b_id to the function by reference. If you defined $b_id in your controller to be a string, then PHP will have to try and convert your query result object ($this->db->query) into a string. It can't do that and therefore throws an error.

Solution: don't pass $b_id to the function in the first place. Why are you doing that anyway?

Also you are doing two queries to try and achieve the same thing?? It is a bit messy. If all you want is to get the details for id = $p_id, just do:
Code:
function get property_id ($p_id)
{
$this->db->where('id', $p_id);
$q = $this->db->get('properties');
if ($q->num_rows() != 1) return false;
return $q->row();
}



Object of class CI_DB_mysql_result could not be converted to string - El Forum - 06-09-2010

[eluser]steelaz[/eluser]
What mddd said. Using his example, if you want to get BRANCH_ID:

Code:
$property = $this->manageproperty_model->get_property_details($p_id);

$branch_id = $property->BRANCH_ID;