Log in

View Full Version : get users name from id in another table



queerfm
04-20-2009, 03:01 PM
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.



<?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.

ganu
04-21-2009, 07:22 AM
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

amutha
04-22-2009, 10:07 AM
try this on query of user table


$user ="SELECT * FROM `users` WHERE `id` ='$from'";

i mention the error. if you correct it. i think, it may working... :)