Help retrieving data from a database and using it in a session

I am having trouble retrieving some data from my database.
I’m trying to display a user’s name in the upper corner when they log in. It will say “Welcome, (Name)”
I can get it to display the user’s username, but I want their real name displayed, which they supply when they register.

Here is where the name should display:

[php]<?php
session_start();
if($_SESSION[’$loggedin’] == “yes”) {
?>

Welcome

<?php include('db_variables/myname.php'); $_SESSION['$myname'] = $myname; echo $myname; ?>
Log Out <?php } else { ?>

Log In | Register

<?php } ?>[/php]

And here is where I am attempting to retrieve the name variable:

[php]<?php

session_start();

include (‘config_game_db.php’);

$_SESSION[’$myusername’] = $myusername;

//$sql = “SELECT * FROM users WHERE username = ‘$myusername’”;
//$result = mysql_query($sql) or die (mysql_error());

// while($row = mysql_fetch_array($result))
// {
// $myid= $row[‘id’];
// $myname = $row[‘name’];
// }

$result = mysql_query(“SELECT id,name FROM users WHERE username = ‘$myusername’”);
if (!$result) {
echo 'Could not run query: ’ . mysql_error();
exit;
}
$row = mysql_fetch_row($result);

$myid = $row[0]; // id
$myname = $row[1]; // name

$_SESSION[’$myname’] = $myname;

?>[/php]

(The commented out section is another method I tried to fetch the data)

The second file is the “myname.php” file.

The site displays “Welcome” with no problem once you are logged in. But the problem is it doesn’t display anything where I echo the user’s real name. But like I said before, I can get it to echo the username with no problem, so I don’t get why I can’t get the name to work.

I’ve been scratching my head for two days trying to figure this one simple thing out! I feel like I’m so close but just missing something?

Try echo or printing the array to see if you have the right results

[php]
$row = mysql_fetch_row($result);
$myid = $row[0]; // id$
$myname = $row[1]; // name

echo ‘id =’.$myid.’
’;
echo ‘myname = ‘.$myname.’
’;
print_r[$row];

[/php]

I tried that

(by the way I needed parentheses not brackets after the print_r, right? Cause it showed an error with brackets so I changed to parentheses and then worked)

And no luck, it displays id= and myname= with nothing after them, just blank.

Yes sorry brackets not square brackets

Strange that you do not get a result ?
Does the var $myname have a name assigned to it ?
Are the id and name fields in the mysql db have values

If you have no result you will never get the id or myname set to a var.

Well when you register, the ‘name’ field is a required field so it goes into the database.
It is under the database named ‘users’ and the column is ‘name’
I’m just confused why I can’t bring it up…the value is definitely there in the db.

But the name you chose is this correct as its a variable put just one name instead of the variable in the where clause see if it changes.

I just tested the code and it works fine and prints the array out.
[php]print_r($row);[/php]

Make sure error reporting is on

I’m a little confused…put a name instead of which variable?
And I think I do have error reporting? In some places anyway…I don’t know how else to get more error reporting.

Does this have anything assigned ot it ?
[php]$myusername[/php]

just change it to and real name from the database and see if you get a result, or echo it out to make sure you get a value assigned to it, there has to be a reason why there is no result from your query if all is right then it should show the array with print_r function.

That worked…it shows the name and id!!

But shouldn’t that variable, $myusername, work…because I have this:

[php]$_SESSION[’$myusername’] = $myusername;[/php]

That session variable is set when you log in, right?
Or does it not work that way?

It depends there maybe nothing assigned to it.
add to session
[php]
$_SESSION[’$myusername’] = $myusername;
[/php]

use session value
[php]$myusername = $_SESSION[’$myusername’];[/php]

Use code to see whats in the session array
[php]print_r($_SESSION);[/php]

This is what it gives me:

Array ( [$myusername] => [$mypassword] => b5c0b187fe309af0f4d35982fd961d7e [$loggedin] => yes [$myname] => Abby )

Looks like nothing is assigned to username?
So how do I get the logged in users username in there?

I guess when you loggin add it there with the var is uses for the username, I do not know because I cannot see your loggin code.

Maybe

[php]
$myusername = $_POST[‘username’];
[/php]

Oh…it looks like it works now…it’s displaying the name.
I think it was because I had

[php]$_SESSION[’$myusername’] = $myusername;[/php]

instead of

[php]$myusername = $_SESSION[’$myusername’];[/php]

Right after I changed that I think that just made it work.

Well done carry on coding ;D

haha Thanks so much for your help.
I am so relieved now that that’s out of my way! :smiley:

Sponsor our Newsletter | Privacy Policy | Terms of Service