Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Display User Name 1

Status
Not open for further replies.

benzito

ISP
Jan 8, 2002
144
0
0
US
I'd like to display a users real name once they log in. ex. "Welcome Joey".
Using the following, I get no output. It doesn't throw an error but doesn't return a value either.
Any idea what I'm doing wrong?

Thanks
===========================================================

session_register('logname','firstName','lastName');


<?
$sql = &quot;SELECT fName FROM `users` WHERE loginName = '$logname'&quot;;
$firstName = fName;
?>


Welcome
<? print $firstName;
?>.



===========================================================
Benzito
PHP newbie: anything I say can and will be used against me!
 
Retrieval of data from a database server requires 4 steps: initiating a connection to the server, selecting the correct database, passing the query to the server, retreiving the data returned from the server.

Assuming your database server is MySQL take a look a the sample code on the following two pages:
Want the best answers? Ask the best questions: TANSTAAFL!
 
You have to select the field you want to match also in the sql statement:

$sql = &quot;SELECT fName FROM `users` WHERE loginName = '$logname'&quot;;
TO

$sql = &quot;SELECT fName, loginName FROM `users` WHERE loginName = '$logname'&quot;;
DeZiner
Never be afraid to try something new.
Remember that amateurs built the Ark.
Professionals built the Titanic
 
Thanks for the replies. Sorry for the sparse post. I've already established a good connection and selected the correct database. I've adjusted the query per below, but now I'm getting &quot;Resource id #2&quot; as the output. Haven't had a chance to &quot;google it&quot; but will when I get home. Am I missing a step?

<?
$Query = &quot;SELECT fName FROM `users` WHERE loginName = '$logname'&quot;;
$Result = mysql_db_query($Query);
?>Welcome<?
print $Result;
?>.

Output = Welcome Resource id #2.
 
DeZiner,

I've tried it with &quot;SELECT fName, loginName FROM `users` WHERE loginName = '$logname'&quot;;
but still get Resource id #2 ... Appears the problem is with $Result = mysql_db_query($Query); ?
 
$Result isn't something to print (except maybe with print_r), you should print the name with something like print $result['fname']
 
You can't print $Result, because it is a handle to the data. Just like you can't just open a handle to a file, then print the handle to get the file's data, you must perform specific actions against the handle to fetch the data.

benzito:
You have missed the 4th step: retrieving dat form the server. You are not using mysql_fetch_row(), mysql_fetch_array(), mysql_fetch_assoc(), or mysql_fetch_object() to actually retreive the information through the resource handle.

Again, please take a look at the example code in the two links I showed you. Both show all the steps necessary to gain access to your data. Want the best answers? Ask the best questions: TANSTAAFL!
 
Thanks sleipnir214,

I took a look at the links and some other resources and now I understand how result and fetch work.

I did the following and it works perfectly.

<?
$result = mysql_query(&quot;SELECT fName FROM `users` WHERE loginName = '$logname'&quot;);
while ($row = mysql_fetch_array($result)) {
$firstName = $row['fName'];
}
?>
Welcome
<? print $firstName;
?>.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top