bruckerrlb Posted August 3, 2007 Share Posted August 3, 2007 Hello, 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! Quote Link to comment https://forums.phpfreaks.com/topic/63197-using-session-data-to-query-a-database/ Share on other sites More sharing options...
cmgmyr Posted August 3, 2007 Share Posted August 3, 2007 try... select * from users where userid = $_SESSION['userid'] Quote Link to comment https://forums.phpfreaks.com/topic/63197-using-session-data-to-query-a-database/#findComment-314979 Share on other sites More sharing options...
stlewis Posted August 3, 2007 Share Posted August 3, 2007 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: <?php //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. $user_id=$_SESSION['user'] //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 Quote Link to comment https://forums.phpfreaks.com/topic/63197-using-session-data-to-query-a-database/#findComment-314981 Share on other sites More sharing options...
bruckerrlb Posted August 3, 2007 Author Share Posted August 3, 2007 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? Quote Link to comment https://forums.phpfreaks.com/topic/63197-using-session-data-to-query-a-database/#findComment-315075 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.