1 Replies - 569 Views - Last Post: 26 March 2013 - 01:43 PM Rate Topic: -----

#1 fatihmert  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 43
  • Joined: 04-March 12

{CodeIgniter}[ERR][Q] db result stdClass object not to string type

Posted 26 March 2013 - 01:33 PM

Hi guys, I'm new to the (dic)php forum. I am learning CodeIgniter(2.1.3).

views/fatih_W.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
	<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
	<title><?php echo $baslik;?></title>
</head>
<body>
	<h1><?php echo $etiket;?></h1><br>
	<b><?php echo $mesaj;?></b>
	<p>Asal sayılar;</p>
	<ul>
	<?php
			foreach($asal_sayilar as $sayi){
				echo "<li>$sayi</li>";
			}
	?>
	<p>Eklenen son 10 bilgi</p>
	<?php
		print_r($sorgu);
	?>
	</ul>
</body>
</html>



controllers/fatih.php

<?php

class Fatih extends CI_Controller{

		public function index(){
			$veri = array(
				"baslik" => "Sayılar",
				"etiket" => "Matematik",
				"mesaj" => "Bu sayılar bitmez"
			);
			$veri["asal_sayilar"] = array(
				"3","5","7","9","11","13"
			);
			$this->load->model('FatihM','',TRUE);
			$veri["son10"] = $this->FatihM->son10_girdi();
			$this->load->view('fatih_W',$veri);

			/*
			########################################
			Is it possible to overload the existing view component?
			$son10 = $this->FatihM->son10_girdi();
			$this->load->view('fatih_W',$son10);
			########################################
			*/
		}
}

?>



models/fatihM.php

<?php
class FatihM extends CI_Model{
	var $baslik = '';
	var $icerik = '';
	var $tarih = '';
	
	function __construct(){
		parent::__construct();
	}
	
	function son10_girdi(){
		$sorgu = $this->db->get("girdiler",1);
		return $sorgu->result();
	}
	
	function girdi_ekle(){
		$this->baslik = $_POST['baslik'];
		$this->icerik = $_POST['icerik'];
		$this->tarih = time();
		
		$this->db->insert('girdiler',$this);
	}
	
	function girdi_gunceller(){
		$this->baslik = $_POST['baslik'];
		$this->icerik = $_POST['icerik'];
		$this->tarih = time();
		
		$this->db->update('girdiler',$this, array(('id') => $_POST['id']));
	}
}
?>



views/fatih_W.php --> print_r($son10);
Information pulled from the database.
Array ( [0] => stdClass Object ( [id] => 1 [baslik] => Bilinmiyor [icerik] => Bilinse güzel olur biliyorum.. [tarih] => 26.03.2013 / 21:20 ) )



Is a single line, easy read to information

foreach($son10[0] as $i){ echo $i;}



output 1BilinmiyorBilinse güzel olur biliyorum..26.03.2013 / 21:20

If in this event, I don't edit my information. Also, although there is no way for it I want to shed more than one line. Therefore, I must find the another way.

I try to like this, however;

foreach($son10 as $i){ echo $i;}



I get an error; Object of class stdClass could not be converted to string

I know the problem. Controls in the description of our roots. Here is the root;

$veri["son10"] = $this->FatihM->son10_girdi();



Looked after, mylook001:unsuccsesful; After I got here, this helped a little but, I can't use num_rows() function.

for($i=0;$i<$son10->num_rows();$i++){
	foreach($son10[$i] as $son){
		echo $son;
	}
}



Fatal error: Call to a member function num_rows() on a non-object in C:\xampp\htdocs\CodeIgniter\application\views\fatih_W.php on line 20

or, basically;

echo $son10->num_rows();



Fatal error: Call to a member function num_rows() on a non-object in C:\xampp\htdocs\CodeIgniter\application\views\fatih_W.php on line 20

What's suggestions in the front of these events? or What is the solution?

Thank you for read and interest.
Good works..

Is This A Good Question/Topic? 0
  • +

Replies To: {CodeIgniter}[ERR][Q] db result stdClass object not to string type

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3479
  • View blog
  • Posts: 10,020
  • Joined: 08-June 10

Re: {CodeIgniter}[ERR][Q] db result stdClass object not to string type

Posted 26 March 2013 - 01:43 PM

explanation(s):
  • $con10 is an array whose first (and only) element is an object of stdClass (mentioned in the dump)
  • in your first foreach() loop you iterate over the properties of the stdClass object, which gives the observed output (and since there is no other output between those echo calls (like newlines, etc.), the output is created in a single line).
  • a stdClass (or any other) object cannot be converted to a string, unless there is a __toString() method in it (which by default is not)
  • in your second foreach() example you try exactly that, converting an object to a string, hence the error.
  • later you try to call the num_rows() method of the $con10 object, but num_rows() is a mysqli method and $con10 is not an object but an array.
  • stdClass does not have any (default) methods to begin with.

This post has been edited by Dormilich: 26 March 2013 - 01:51 PM

Was This Post Helpful? 2
  • +
  • -

Page 1 of 1