show users name

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dru_nasty
Forum Commoner
Posts: 81
Joined: Sat Sep 10, 2005 10:26 am

show users name

Post by dru_nasty »

I'm trying to show a users name from the database on a page within a members area of the site.
This is what I've got going so far. A connection to the database, a variable $username that stores the users username and a sql query for the persons name.

can someone help me out some?

Code: Select all

<?
include_once('/usr/home/fss/websites/fivestarsports.net/inc/func.inc.php');
$username = $HTTP_COOKIE_VARS['log']['username'];
$db_conn = five_connect_db();
mysql_select_db('fivestar',$db_conn);
$query = "select fname from registered_mem where username = $username";
?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

use sessions to store the information instead of a cookie (less dangerous, more often) .. at any rate, you need to put quotes around $username in your query string (and properly escape it)
dru_nasty
Forum Commoner
Posts: 81
Joined: Sat Sep 10, 2005 10:26 am

Post by dru_nasty »

could you please give me an example
comrad
Forum Newbie
Posts: 6
Joined: Tue Oct 25, 2005 2:35 pm
Location: Cambridge, MA

Post by comrad »

Code: Select all

<?
include_once('/usr/home/fss/websites/fivestarsports.net/inc/func.inc.php');

$username = $HTTP_COOKIE_VARS['log']['username'];
$db_conn = five_connect_db();
mysql_select_db('fivestar',$db_conn);

$query = "select fname from registered_mem where username = $username";
//------------------
     $result=mysql_query("$query");
    $my=mysql_fetch_assoc($result);
echo"  
My Fname is $my[fname] .<br>
My username is $username <br>";
?>
dru_nasty
Forum Commoner
Posts: 81
Joined: Sat Sep 10, 2005 10:26 am

Post by dru_nasty »

Thanks for providing this, i'll play around with it tomorrow.
I've had enough for tonight.
I'll let you know how it goes.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

comrad wrote:

Code: Select all

<?
include_once('/usr/home/fss/websites/fivestarsports.net/inc/func.inc.php');

$username = $HTTP_COOKIE_VARS['log']['username'];
$db_conn = five_connect_db();
mysql_select_db('fivestar',$db_conn);

$query = "select fname from registered_mem where username = $username";
//------------------
     $result=mysql_query("$query");
    $my=mysql_fetch_assoc($result);
echo"  
My Fname is $my[fname] .<br>
My username is $username <br>";
?>

More like this...

Code: Select all

<?
// Use sessions, include this on all sessioned pages
session_start();

include_once('/usr/home/fss/websites/fivestarsports.net/inc/func.inc.php');
// Replace COOKIE var with SESSION var
//$username = $HTTP_COOKIE_VARS['log']['username'];
/*
 * THIS ASSUMES THE SESSION VAR WAS SET PRIOR TO THIS
 */
$username = $_SESSION['username'];
$db_conn = five_connect_db();
mysql_select_db('fivestar',$db_conn);

$query = "select fname from registered_mem where username = $username";
$result=mysql_query($query);
while ($my=mysql_fetch_array($result))
{
    echo "My Fname is " . $my['fname'] . "<br />\n";
    echo "My username is $username.<br />\n";
}
?>
mickd
Forum Contributor
Posts: 397
Joined: Tue Jun 21, 2005 9:05 am
Location: Australia

Post by mickd »

dont you mean

Code: Select all

$query = "select fname from registered_mem where username = '$username'"; // notice the single quote around the $username...
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Yup, I overlooked that. Good catch.
Post Reply