<?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
Tarık BaysalTarık Baysal
asked Sep 22, 2016 at 21:49
1 Answer 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
lang-php
$row
is an object of classlink
.class Dizi
have to do with this? Did you mean to useDizi
instead oflink
insetFetchMode
?