Jump to content

Using two mysql tables


[-razer-]Blade

Recommended Posts

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
Share on other sites

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
Share on other sites

Yes, I have been reading my my php and mysql for dummies book ;D 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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.