So I am very bad at this anyway right now I have this code
$test = "SELECT `status`,`pubdate` FROM `status` ORDER BY `pubdate` DESC";
$stmt = $db->prepare($test);
$stmt->execute();
while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
echo "<section class='statusar'>"."<article>";
echo $row['status'];
echo $row['pubdate'];
echo "</article></section>";
}
and it shows the post but now I want to take users first name from another table and display it with the post. How do I do it?
The two tables I have look like this
This is the table with the post
Status_id int unsigned auto_increment primary
user_id int unsigned index
status varchar(600)
pubdate datetime
This is the other table
user_id int unsigned auto_increment primary index
firstname varchar(30)
surname varchar(30)
username varchar(30) unique
password varchar(90)
I have done so they have a relation between the user_ids
EDIT
I think I figured it out because now it works :D So now when I use
SELECT status, pubdate,firstname,surname FROM status INNER JOIN user ON status.user_id = user.user_id ORDER BY status.pubdate DESC
It displays all the things I want and I also tried
SELECT * FROM status INNER JOIN user ON status.user_id = user.user_id ORDER BY status.pubdate DESC";
Which also works thanks for the help guys :D
Just use an inner join
:
SELECT s.status, s.pubdate, u.firstname
FROM status s
INNER JOIN usertable u ON s.user_id = u.user_id
ORDER BY s.pubdate DESC
This assumes the user_id
exists in the usertable
. If that might not be the case, you'd want to use an outer join
instead.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments