Yunus Yunus - 13 days ago 9
MySQL Question

I wanna use Join with PDO But There is a Error

I wanna use join with PDO but there is an error. I research other questions but I don't understand :( My query and error is;

ERROR

PHP Fatal error: Call to a member function rowCount() on a non-object in /ssd4/muhtesemciftlik/public_html/referans.php on line 72


MY QUERY

<?php
$id=2
$query = $db->query("SELECT ref_urun,ref_tl FROM degerler INNER JOIN kullanicilar ON kullanicilar.ref = degerler.kullanici WHERE kullanicilar.ref = {'$id'}", PDO::FETCH_ASSOC);

if ( $query->rowCount() ) { //kayıt varsa

foreach( $query as $row ){
echo "
<tr>
<th style=\"width: 10px\">#</th>
<th>".$row["kullanici"]."</th>
<th>".$row["ref_urun"]."</th>
<th>".$row["ref_tl"]."</th>
</tr>
";
}

}
?>

Answer

Change:

$query = $db->query("SELECT ref_urun,ref_tl FROM degerler INNER JOIN kullanicilar ON kullanicilar.ref = degerler.kullanici WHERE kullanicilar.ref = {'$id'}", PDO::FETCH_ASSOC);

To:

$query = $db->query("SELECT ref_urun,ref_tl FROM degerler INNER JOIN kullanicilar ON kullanicilar.ref = degerler.kullanici WHERE kullanicilar.ref = '{$id}'", PDO::FETCH_ASSOC);