Results 1 to 3 of 3

Thread: get users name from id in another table

  1. #1
    Join Date
    May 2006
    Posts
    266
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default get users name from id in another table

    Hi I have two table one called user and the other call msg.

    In msg i have from_id, to_id and read

    I am waiting to get the username of the from_id and it on the users page so the user knows who sent the message. at the moment it is only showing the users ID.

    I tryed the following but it did not work.

    Code:
    <?php
    $sql = 'SELECT * FROM `msg`';
    $query = mysql_query($sql) or die(mysql_error());
    while($row = mysql_fetch_assoc($query))
    {  
      $from = $row[from_id];
      $to_id = $row[to_id];
      $msgid = $row[msgid];
    
    	$user = 'SELECT * FROM `users` WHERE `id` ='.$from;
    	$userquery = mysql_query($user);
    	while($usermsg = mysql_fetch_array($userquery))
    	{
    	$msg_from_image = $usermsg[username];
    
      ?>
    From	|   reply?	| Delete
    <form>
    <?php echo $msg_from_image;?>
    <input id="delete" value="0" name="to" size="10" Type="hidden"> <input class="button" onClick="ajaxFunction();" value="Enter" type="button" >
    </form>
    
    <?php
      }  
    }
    ?>
    If any one can help that would be great.
    The web in one word.

  2. #2
    Join Date
    Apr 2009
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    i think there is no matching between id in users table and from_id in msg table,
    means in user table the id values are 1,2,3,45.
    and in msg table the from_id values are 0,6.7,8
    ex:
    SELECT * FROM `users` WHERE `id` ='6';
    id 6 is not in users table

  3. #3
    Join Date
    Apr 2009
    Location
    India
    Posts
    41
    Thanks
    0
    Thanked 4 Times in 4 Posts

    Default

    try this on query of user table

    Code:
    $user ="SELECT * FROM `users` WHERE `id` ='$from'";
    i mention the error. if you correct it. i think, it may working...

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •