0
<?php
include "baglan.php";
class Dizi { 
public function dizis(){
 return $this->dizi;
}
}
$query2 = $db->query("SELECT * FROM diziler ");
$query2->setFetchMode(PDO::FETCH_CLASS, 'link');
foreach($query2 as $row){
 echo $row;
 break;
}
?>

I think I did everything right but I receive error:

Notice: Array to string conversion in C:\xampp\htdocs\xampp\dizimag\new.php on line 14

Array

Tried everything but I guess I do something wrong with foreach but I couldn't find it anywhere.

 class Uye {
 public function adsoyad(){
 return $this->uye_ad . ' ' . $this->uye_soyad;
 }
 public function rutbe(){
 if ( $this->uye_rutbe == 1 )
 return 'Yönetici';
 else
 return 'Üye';
}
}
$query = $db->query("SELECT * FROM uyeler");
$query->setFetchMode(PDO::FETCH_CLASS, 'Uye');
foreach ( $query as $row ){
print $row->adsoyad() . ': ' . $row->rutbe() . '<br />';
}

this code works perfect. Where did i make mistake? i did same things ? :S

asked Sep 22, 2016 at 21:49
4
  • Which line is line 14? Commented Sep 22, 2016 at 21:51
  • $row is an object of class link. Commented Sep 22, 2016 at 21:51
  • What does class Dizi have to do with this? Did you mean to use Dizi instead of link in setFetchMode? Commented Sep 22, 2016 at 21:52
  • Possible duplicate of stackoverflow.com/questions/5137051/pdo-php-fetch-class Commented Sep 24, 2016 at 12:03

1 Answer 1

1

You receive the Array to string conversion error because PDO is returning $row as an array in line 14:

foreach($query2 as $row){
 echo $row; // <- this is an array, echo'ing it will fail
 break;
}

To learn how to use PDO::FETCH_CLASS, I suggest that you study the examples given here:

answered Sep 24, 2016 at 12:02
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.