[-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. Link to comment https://forums.phpfreaks.com/topic/81494-using-two-mysql-tables/ 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 Link to comment https://forums.phpfreaks.com/topic/81494-using-two-mysql-tables/#findComment-413734 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); Link to comment https://forums.phpfreaks.com/topic/81494-using-two-mysql-tables/#findComment-413735 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 } ?> Link to comment https://forums.phpfreaks.com/topic/81494-using-two-mysql-tables/#findComment-413738 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.