Abhi Burk Abhi Burk - 4 months ago 9
PHP Question

How Do I Search Data From Two Tables using Join

This is My SELECT Query For searching user from the DB. The problem is I am only able to get the search result of those users who have posted any content in their

postImg
table. Otherwise there is no user found. I want to search in such a way that even if any user has not posted any post then it should be found. I am using this query:

<?php
$sql = "SELECT * FROM postImg LEFT JOIN user ON postImg.userID=user.userID
WHERE user.userID='".$page_owner."' AND (userName LIKE '%".$searchVal."%') GROUP BY user.userID ";
$res3 = mysql_query($sql) or die(mysql_error());
while($row2=mysql_fetch_assoc($res3)){
echo $row2['userName']; // From user Table

echo $row2['likes']; // From postImg table
echo $row2['posts']; // From postImg table
} ?>

Answer
$sql =  "SELECT * FROM postImg RIGHT JOIN user ON postImg.userID=user.userID 
    WHERE user.userID='".$page_owner."' AND (userName LIKE '%".$searchVal."%') GROUP BY user.userID ";
Comments