[-razer-]Blade Posted December 13, 2007 Share Posted December 13, 2007 Ok I am trying to display my news and who posted it. But when the news is entered into my database, instead of posting the users name in there I put their ID. So what I want to do is match up their id with the one in the news table so it will then display their username instead of their id. The reason I do it this way is in case the member ever changes their name it will automatically be updated in the news table as well. any help is appreciated. Quote Link to comment Share on other sites More sharing options...
rajivgonsalves Posted December 13, 2007 Share Posted December 13, 2007 you have to use database joins http://dev.mysql.com/tech-resources/articles/intro-to-normalization.html#07 Quote Link to comment Share on other sites More sharing options...
marcus Posted December 13, 2007 Share Posted December 13, 2007 You can create a function I suppose. I do this with my website. function uname($id){ $sql = "SELECT username FROM `users` WHERE `id`='".mysql_real_escape_string($id)."'"; $res = mysql_query($sql) or die(mysql_error()); if(mysql_num_rows($res) == 0){ return "Invalid User"; }else { $row = mysql_fetch_assoc($res); return $row['username']; } } echo uname(5); Quote Link to comment Share on other sites More sharing options...
[-razer-]Blade Posted December 13, 2007 Author Share Posted December 13, 2007 Yes, I have been reading my my php and mysql for dummies book about the table joins but for some reason they just confuse me :'( maybe this will help. Here is the code I was using before I made my database like this. $query = mysql_query("SELECT * FROM news ORDER BY news_id DESC LIMIT 0,5"); while($row = mysql_fetch_array($query)) { $news_id = $row['news_id']; $news_title = $row['news_title']; $news_text = $row['news_text']; $news_postedby = $row['news_postedby']; $news_date = $row['news_date']; ?> <table width="100%" border="0" cellspacing="0" cellpadding="0"> <tr> <td width="12"><img src="images/content_top_left.png" width="12" height="12" alt="" /></td> <td class="content_top"></td> <td width="12"><img src="images/content_top_right.png" alt="" width="11" height="12" /></td> </tr> <tr> <td class="content_left"></td> <td class="content_main"><div id="header" align="center"><?php echo $news_title; ?></div> <?php echo $news_text; ?> <div align="center">Posted by:<?php echo $news_postedby; ?> on <?php echo $news_date; ?></div></td> <td class="content_right"></td> </tr> <tr> <td width="12"><img src="images/content_bottom_left.png" alt="" width="12" height="25" /></td> <td class="content_bottom"></td> <td width="12"><img src="images/content_bottom_right.png" alt="" width="11" height="25" /></td> </tr> </table> <?php } ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.