yahoo5000 yahoo5000 - 6 months ago 16
PHP Question

mysql query error 3 table JOIN

Hello i am trying to join 3 tables and get all information from them table names : fav,images,posts

fav table stores: userid,postid (this table is for favorite ads)

images table stores: postid,path(1-5)

posts table stores all information about post : id,userid,title etc

so now i am doing sql query witch should select everything from fav and joing post and images where

$_session["userid"] = to userid

this how its looks like :

<?php

$userid = $_SESSION["userid"];

$sql = "SELECT * FROM fav f JOIN posts p ON f.postid = p.id JOIN images i ON p.id = i.postid WHERE userid='$userid' ";
$res = mysqli_query($connect,$sql);
while ($row = mysqli_fetch_assoc($res)) {
$postid = $row["postid"];
?>
<div id="ads">
<div id="titlepic">
<a href="review.php?id=<?php echo $postid; ?>"><?php echo $row["title"]; ?></a><br>
<a href="review.php?id=<?php echo $postid; ?>"><img src="<?php if(!empty($row["path1"])) { echo $row["path1"]; } else echo "image/noimage.png"; ?>" height="100px" width="150px"></a>
</div>

<div id="dates">
<b>Date Added:</b> <?php echo date('m/d/Y H:i', $row["dateadded"]); ?><br>
<b>Renew Date:</b> <?php if($row["renewdate"] > 0){ echo date('m/d/Y H:i', $row["renewdate"]); } ?><br>
<b>Location:</b> <?php echo $row["location"]; ?><br>
<b>Price:</b> <?php echo $row["price"]."&pound"; ?><br>

</div>
</div>
<hr width="100%">
<?php
}

?>


but i get error after i put
WHERE userid='$userid'
error sounds like this
"Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given in C:\wamp\www\project\fav.php on line 129"


where is the problem i don't get it cause if i don't do WHERE part everything works without warnings and errors

Answer

Change query to

 $sql = "SELECT f.*,p.*,i.* FROM fav f JOIN posts p ON f.postid = p.id JOIN images i ON p.id = i.postid WHERE f.userid='$userid' ";

I assume userid belongs to fav f change it to p.userid or i.userid if it belongs to these table.