BustahBrown Posted April 9, 2010 Share Posted April 9, 2010 I've got this code to display the latest created article. In my users table i've got "id" which is the primary key, first name, last name, username and password. In my articles table i've got article id, user id, category, title, content, cover, bigcover and date. I've got the article displaying everything it needs to, however where it's got ".$row1['userid']." in the content1date div, i would like it to display the username from the users table. (id from the users table and userid from the articles table is the relation) <?php $article1 = mysql_query("SELECT * FROM articles ORDER BY id DESC LIMIT 0, 1"); while ($row1 = mysql_fetch_assoc($article1)) { echo " <div id='content1image'><img src='upload/".$row1['cover']."' /></div> <div id='content1title'><font size='5' face='verdana'><i>".$row1['title']."</i></font></div> <div id='content1date'><font size='2' face='verdana'>Posted in <b><a href=''>".$row1['category']."</a></b> by <b><a href=''>".$row1['userid']."</a></b> on ".$row1['date']."</font></div> <div id='content1'><font size='2' face='verdana'>".$row1['content']."</font></div> <div id='sub1'><font face='Verdana' size='5'><i>".$row1['title']."</i></font></div> "; } ?> There's probably some really simple solution, unfortunately i'm fairly new to PHP, any help would be awesome, thanks Link to comment https://forums.phpfreaks.com/topic/198093-replacing-a-userid-with-a-username-from-another-table/ Share on other sites More sharing options...
cs.punk Posted April 9, 2010 Share Posted April 9, 2010 What your looking for is a JOIN mysql query... Sorry not to familiar with them myself.. Link to comment https://forums.phpfreaks.com/topic/198093-replacing-a-userid-with-a-username-from-another-table/#findComment-1039360 Share on other sites More sharing options...
BustahBrown Posted April 9, 2010 Author Share Posted April 9, 2010 Ah thanks, yeah i figured it was something like this, i've tried sticking in some joins, but honestly not where where to stick them in (if it's in my while statement where i think it should go it stuffs up) So anyone that's good with joins, would be muchly appreciated! Link to comment https://forums.phpfreaks.com/topic/198093-replacing-a-userid-with-a-username-from-another-table/#findComment-1039361 Share on other sites More sharing options...
cs.punk Posted April 9, 2010 Share Posted April 9, 2010 Well its quite simple actually.. (Just found out now..) <?php $article1 = mysql_query("SELECT * FROM articles, users WHERE articles.userid = users.userid ORDER BY id DESC LIMIT 0, 1"); ?> Link to comment https://forums.phpfreaks.com/topic/198093-replacing-a-userid-with-a-username-from-another-table/#findComment-1039363 Share on other sites More sharing options...
BustahBrown Posted April 9, 2010 Author Share Posted April 9, 2010 Ah, this is what i tried earlier. When i put it in i get this error "Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\pp\index.php on line 11" which is my while loop Link to comment https://forums.phpfreaks.com/topic/198093-replacing-a-userid-with-a-username-from-another-table/#findComment-1039366 Share on other sites More sharing options...
isedeasy Posted April 9, 2010 Share Posted April 9, 2010 Try... "SELECT a.id, a.userid, a.category, a.title, a.content, a.cover, a.bigcover, a.date, u.username FROM articles a JOIN users u ON u.id = a.userid ORDER BY a.id DESC LIMIT 0, 1" Link to comment https://forums.phpfreaks.com/topic/198093-replacing-a-userid-with-a-username-from-another-table/#findComment-1039386 Share on other sites More sharing options...
BustahBrown Posted April 9, 2010 Author Share Posted April 9, 2010 That's done it! thankyou so much! Link to comment https://forums.phpfreaks.com/topic/198093-replacing-a-userid-with-a-username-from-another-table/#findComment-1039813 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.