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

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Object of class CI_DB_mysql_result could not be converted to string [SOLVED] (/showthread.php?tid=16560)



Object of class CI_DB_mysql_result could not be converted to string [SOLVED] - El Forum - 03-10-2009

[eluser]Asinox[/eluser]
Hi guys, im trying to count register, but i have the next error:
Code:
A PHP Error was encountered
Severity: 4096
Message: Object of class CI_DB_mysql_result could not be converted to string
Filename: controllers/site.php
Line Number: 41

my code
Code:
function columnasTiposBares(){
             $tipos = $this->modsite->tiposBaresColumna();
             foreach($tipos as $tipo):
             $contando = $tipo->tipo;
             $tip[] =  anchor('site/lugares/'.$tipo->tipo,$tipo->tipo)."->".$this->db->query('SELECT COUNT(tipo) as total from negocios where tipo = "'.$tipo->tipo.'"');
             endforeach;
         return  $this->table->make_columns($tip, 3);
      }

What is wrong guys?

Thanks


Object of class CI_DB_mysql_result could not be converted to string [SOLVED] - El Forum - 03-10-2009

[eluser]TheFuzzy0ne[/eluser]
Which is line 41?


Object of class CI_DB_mysql_result could not be converted to string [SOLVED] - El Forum - 03-10-2009

[eluser]TheFuzzy0ne[/eluser]
OK, I see that this is line 41:
Code:
$tip[] =  anchor('site/lugares/'.$tipo->tipo,$tipo->tipo)."->".$this->db->query('SELECT COUNT(tipo) as total from negocios where tipo = "'.$tipo->tipo.'"');
The problem is that you're trying to use the db_result as a string. You need to extract the string you require from the query first.


Object of class CI_DB_mysql_result could not be converted to string [SOLVED] - El Forum - 03-10-2009

[eluser]Asinox[/eluser]
ok, thanks, is working Smile, but how i print the "total" from the query in this line?

$tip[] = anchor('site/lugares/'.$tipo->tipo,$tipo->tipo)." ( ". HERE I NEED THE SUM TOTAL ." )";

Code:
function columnasTiposBares(){
             $tipos = $this->modsite->tiposBaresColumna();
             foreach($tipos as $tipo):
             $contando = $this->db->query('SELECT COUNT(tipo) as total from negocios where tipo = "'.$tipo->tipo.'"');
             $tip[] =  anchor('site/lugares/'.$tipo->tipo,$tipo->tipo)." ( ".$contando->result() ." )";
             endforeach;
         return  $this->table->make_columns($tip, 3);
      }

thanks


Object of class CI_DB_mysql_result could not be converted to string [SOLVED] - El Forum - 03-10-2009

[eluser]Asinox[/eluser]
ok, thanks i did it

Code:
function columnasTiposBares(){
             $tipos = $this->modsite->tiposBaresColumna();
             foreach($tipos as $tipo):
             $contando = $this->db->query('SELECT COUNT(tipo) as total from negocios where tipo = "'.$tipo->tipo.'"');
             $fila= $contando->row();
             $tip[] =  anchor('site/lugares/'.$tipo->tipo,$tipo->tipo)." ( ".$fila->total ." )";
             endforeach;
         return  $this->table->make_columns($tip, 3);
      }

Thanks


Object of class CI_DB_mysql_result could not be converted to string [SOLVED] - El Forum - 03-10-2009

[eluser]TheFuzzy0ne[/eluser]
Code:
$tip[] =  anchor('site/lugares/'.$tipo->tipo,$tipo->tipo)." ( ".$contando->row()->total ." )"; # this will only work in PHP 5



Object of class CI_DB_mysql_result could not be converted to string [SOLVED] - El Forum - 03-10-2009

[eluser]TheFuzzy0ne[/eluser]
Well done. Smile