Forum Moderators: coopster

Message Too Old, No Replies

Display a field in a database based on another field

I need to display a users name based on their username in the same row

         

amdorsey

2:35 am on Mar 25, 2004 (gmt 0)

10+ Year Member



My php/SQL script to display the First Name works, but I need to display their name based on their username obviously or whenever someone logins in they see the first name of the first DB entry, not their own name.

Here is my script can anyone help?
<?php
mysql_select_db($database_DBconnect, $DBconnect);
$query_displayUsersName = "SELECT first_name FROM `user`";
$displayUsersName = mysql_query($query_displayUsersName, $DBconnect) or die(mysql_error());
$row_displayUsersName = mysql_fetch_assoc($displayUsersName);
$totalRows_displayUsersName = mysql_num_rows($displayUsersName);
?>

Elijah

2:51 am on Mar 25, 2004 (gmt 0)

10+ Year Member



Try something like this:

$query_displayUsersName = "SELECT first_name FROM `user` WHERE username='$username'";

Hope this helps you,

Elijah :)

amdorsey

3:13 am on Mar 25, 2004 (gmt 0)

10+ Year Member



Thanks. But I tried that. When I do it that way nothing appears, its just left blank. I thought maybe I would have to modify this line...

$row_displayUsersName = mysql_fetch_assoc($displayUsersName);

But I'm not sure. I can't use session variables because that only captures the 'username'

willybfriendly

3:23 am on Mar 25, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



nothing appears, its just left blank

I am assuming that you mean your echo statement isn't working. What does that statement look like?

WBF

Elijah

3:31 am on Mar 25, 2004 (gmt 0)

10+ Year Member



Does $username have a value? Or is it a session variable?
If so try this:
$username=$_SESSION['username'];
$query_displayUsersName = "SELECT first_name FROM `user` WHERE username='$username'";

amdorsey

3:40 am on Mar 25, 2004 (gmt 0)

10+ Year Member



Thanks dude. I actually just tried that and it worked.
I'm using Dreamweaver so it used changed $username to MM_Username by default so I just added this line, but by the time I came back to let you know you posted the exact solution. Your awesome!

$username = $_SESSION['MM_Username'];
$query_displayUsersName = "SELECT first_name FROM `user` WHERE username = '$username'";
-----------------------------
Hey since I got you here, do you know of anyway to display the date using the set PHP date function and modifying it to display my time. (i.e. my server is in california and I'm on the east coast)

Thanks,

coopster

1:41 pm on Mar 25, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



There are a number of options, but one is to use an offset and a pair of PHP's Date and Time Functions [php.net]:
$time_offset = -2; // Represented in hours 
// 60 seconds in a minute, 60 minutes in an hour:
$two_hours_ago = date [php.net]('H:i:s', time() [php.net] + (60 * 60 * $time_offset));
print $two_hours_ago;