Ethan Hofton Ethan Hofton - 6 months ago 18
SQL Question

SQL query join order by

I'm making and instagram parody and I need to make a feed. I have 2 tables "following" and "Posts".

This is the "following" database:

following database

This is the "posts" database:

Posts database

Here is my code to try and select the people that the user follows and put their posts into their feed:


$username = $_SESSION['username'];
$sql = "SELECT username_follow FROM following WHERE follower = '$username'";
$rsu = mysql_query($sql, $conn);
while ($row = mysql_fetch_array($rsu)){
$uername2 = $row[0];
$sql2 = "SELECT pic, `time` FROM `Posts` WHERE username = '$uername2' ORDER BY `Posts`.`id` DESC";
$res = mysql_query($sql2, $conn);
//echo $row[0];
while ($row2 = mysql_fetch_row($res)) {
echo "<br> <br>";
echo "<center>";
echo "<img width='450px' height='450px' src='data:image;base64,".base64_encode($row2[0])."'>";

echo "<br> <br>";
echo "</center>";


But how can I show the pictures in chronological order?


Join the two tables and order by time

SELECT f.username_follow, p.pic, p.time
FROM following AS f
JOIN Posts AS p ON p.username = f.username_follow
WHERE follower = '$username'
ORDER BY p.time