Jump to content

using session data to query a database


Recommended Posts



Basically the title is the general idea of what i'm trying to do, a user logs in, great, his login id is stored (it is also his number on a seperate database) in a session variable. Now that session variable needs to be queried to another database (with all of that users information) to get all of the information on that user back to the original server (not the first database, the first database just has user number, id, and a password field). So I have a  question, and if you have answers great! or if you know of the thread that they are on that would also help because i've been searching and haven't found anything yet.


1. How can I query a session variable in a sql statement, do I just put select * from users where '.$_SESSION['mysessionname'].' = usernumber ?


Thank you!

Link to comment
Share on other sites

Close, but no cigar :).  A session variable is not really all that different from any other variable, and it could be used in your SQL statement just like any other...However, the syntax would be easier like this:




//Assign the session value to a local variable...just my habit, optional if you want to just use $_SESSION directly...I just think this makes for cleaner coding.

//Create SQL Statement...The SELECT statement has to be formatted like this...caps are optional...again, my habit is to ALL CAP any SQL keywords.

$SQL= "SELECT * FROM mytable WHERE usernumber=".$user_id; 


That easy 

Link to comment
Share on other sites

Thank you! I will try to declare the session variable a regular variable as soon as I figure how the tables work, there seems to be a table for each user, and each product, is there a way to query a database using mysql for a specific table using the $_SESSION variable?

Link to comment
Share on other sites

This thread is more than a year old. Are you sure you have something important to add to it?

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.