Jump to content

retrieving data using a session


greenApple

Recommended Posts

If I set the value of a session to an email address during a login script, can I later use this session on another page to retrieve data from a database which contains the email address? Something like this?

                                 session_start();                                 $_SESSION['username'] = $email;				$get = mysql_query("SELECT firstname FROM user WHERE email='$email'");				                                while ($row = mysql_fetch_assoc($get))//creates an array				{				$id = $row['userId'];                                $firstname = $row['firstname'];                                $etc = $row['etc'];				}

I have tried this aswell

	                          $_SESSION['username'];                          $get = mysql_query("SELECT firstname FROM user WHERE email='$_SESSION'");

this code doesn't work for me so think I must be wrong. Can I store a number of details in one session or how can I get around this?

Link to comment
Share on other sites

You can assign to as many indices of the $_SESSION array as you like. Are you calling session_start() at the top of all your pages?Edit: eh, hang on, you've got this the wrong way round: $_SESSION['username'] = $email;. Remember, $_SESSION is an array, you use it like all other arrays. Your second code should look like:

session_start();$get = mysql_query("SELECT firstname FROM user WHERE email='{$_SESSION['username']}'");

Link to comment
Share on other sites

Yes I am calling session_start() on every page.I have changed the code around but the variables ($id, $firstname etc.) don't hold any value, nothing displays when I echo them out :)

session_start();$get = mysql_query("SELECT firstname FROM user WHERE email='{$_SESSION['username']}'");while ($row = mysql_fetch_assoc($get))//creates an array{$id = $row['userId'];$firstname = $row['firstname'];$etc = $row['etc'];}echo $id;

Link to comment
Share on other sites

Well, try echoing $_SESSION['username'], see whether it contains what it does. Then echo out the query to see whether that contains what it does. Etc., and you should eventually find the problem. :)

Link to comment
Share on other sites

echo $_SESSION['username'] gives me the correct user emailif I echo the query echo $get; I get nothingI'm lost as to what is wrong here!Is this code correct? Is '{$_SESSION['username']}' correct? It is highlighted differently in my text editor compared to other variables, are the quotations correct?

$get = mysql_query("SELECT * FROM user WHERE email='{$_SESSION['username']}'");

Link to comment
Share on other sites

In your other example -- your first example -- the sql statement is in need of the $email var being populated with an actual email address value. And you obviously knew that. But try flipflopping/changing what you coded on each side of the equals sign in the assignment statement just before the sql.Change it from how you had it$_SESSION['username'] = $email;To$email = $_SESSION['username'] That assigns the email address stored in the session variable from right to left into $email.And if that doesn't work test the sql by itself. First by temporarily changing what is now on the right side of the equals sign with a hardcoded email address of your choice. Something in your database maybe.At that point you should be proving out the sql works the way you want it.Let me know

Link to comment
Share on other sites

echo $_SESSION['username'] gives me the correct user emailif I echo the query echo $get; I get nothingI'm lost as to what is wrong here!Is this code correct? Is '{$_SESSION['username']}' correct? It is highlighted differently in my text editor compared to other variables, are the quotations correct?
$get = mysql_query("SELECT * FROM user WHERE email='{$_SESSION['username']}'");

$get is not a query actually. output the SELECT * FROM user WHERE email='{$_SESSION['username']}'". $get holds the return value of executed query.if somehow your query fails it will evaluate false and it will not echo anything. your query is looking correct but may be you are missing something which causes the query failure (eg pulling data from a table which does not exist).so checking the output of query and checking the $get for error(if exist) and echoing it by mysql_error() would help you.
Link to comment
Share on other sites

Thank you for all your help, I really do appreciate everything! This forum is great, well the people are! There were two issues with my code.

Change it from how you had it$_SESSION['username'] = $email;To$email = $_SESSION['username'] That assigns the email address stored in the session variable from right to left into $email.
Swapping this around worked :)
so checking the output of query and checking the $get for error(if exist) and echoing it by mysql_error() would help you.
I had a problem with the connection string which showed up when I used mysql_error(). I've learnt the hard way to always use error checking :) Thanks :):(
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...