PHP & SQL - Converting user_id into username?

cammy

Member
May 15, 2014
481
238
I'm currently trying to display the latest 6 photos taken by users as shown below...
bRns7gP.png

But I don't know how to convert the user_id to the persons username. Nor do I know how to convert the timestamp to date and time.

Here's the current script.
<?php
$sql = $dbh->prepare("SELECT id,user_id,timestamp,url FROM camera_web ORDER BY id DESC LIMIT 6");
$sql->execute();
while ($photos = $sql->fetch())
{
echo'
<div class="boxphotos">
<a href="/news/'.filter($photos["id"]).'"><div class="photosimage" style="background-image: url('.filter($photos["url"]).');">
<div class="photosuser">
'.filter($photos["user_id"]).'
</div>
<div class="photostime">
'.filter($photos["timestamp"]).'
</div>

</div></a>
</div>';
}
?>
 

GertjanA

New Member
Jan 23, 2019
4
3
You could use an inner join on the SQL like SELECT t1.* and t2.user and merge the two tables.
Feel free to add me on discord so I can help you build the query for your need. (GertJanFRL#1527)
 

Berk

berkibap#4233
Developer
Oct 17, 2015
863
190
SQL:
SELECT camera_web.*, users.username FROM camera_web JOIN users ON camera_web.user_id = users.id ORDER BY camera_web.id DESC
try this,and adjust how you want:)
 

Users who are viewing this thread

Top