dan_t Posted June 16, 2010 Share Posted June 16, 2010 Hello everyone, I'm having a little trouble here. I'm trying to use a session created from log in(the user id) and take that user id and pull out information from the data base. An example would be: (this is not the actual code, but I will wrap it anyway) $_session['username']; $userId = 'username'; $query ="SELECT * FROM databasetable where userId = (whatever I need to draw out)"; ( **Here's where the confusion comes in.** How would I take the $userId variable from the users login and get information such as sex being male or female, and then print userId's of the opposite sex? **) So far I have tried about a hundred different ways with no luck, now I turn to the experts. So I have totally confused myself - I hope I am asking the question coherently Hopefully you can figure out what I'm looking for. Quote Link to comment https://forums.phpfreaks.com/topic/204905-pulling-information-from-session-id/ Share on other sites More sharing options...
trq Posted June 16, 2010 Share Posted June 16, 2010 Its not really much help not seeing actual code but something like this will likely help you. $query ="SELECT * FROM databasetable where userId = " . $_SESSION['userid']; Not that the $_SESSION array is all caps. Quote Link to comment https://forums.phpfreaks.com/topic/204905-pulling-information-from-session-id/#findComment-1072724 Share on other sites More sharing options...
dan_t Posted June 16, 2010 Author Share Posted June 16, 2010 I don't have an actual code yet. I'm just trying to figure out how to do it. I've tried a few different ways, but none worked. Quote Link to comment https://forums.phpfreaks.com/topic/204905-pulling-information-from-session-id/#findComment-1072726 Share on other sites More sharing options...
Soldier Jane Posted June 16, 2010 Share Posted June 16, 2010 I'd suggest posting the code you've tried then? Make sure you call session_start() near the beginning of your code as well. $_SESSION is a superglobal associative array, so as thorpe suggested, make sure it's in caps every time you call on it. Quote Link to comment https://forums.phpfreaks.com/topic/204905-pulling-information-from-session-id/#findComment-1072729 Share on other sites More sharing options...
dan_t Posted June 16, 2010 Author Share Posted June 16, 2010 I deleted each one after I tried and failed. I will try another and post it. Quote Link to comment https://forums.phpfreaks.com/topic/204905-pulling-information-from-session-id/#findComment-1072732 Share on other sites More sharing options...
dan_t Posted June 16, 2010 Author Share Posted June 16, 2010 Before I try and fail again - is there a good way to draw the opposite of a users attribute in a database? like: $query = "SELECT databasetable.sex where $userId.sex != $_SESSION['userId'].sex I realize this would never work, but can you kind of see what I'm going after? Quote Link to comment https://forums.phpfreaks.com/topic/204905-pulling-information-from-session-id/#findComment-1072735 Share on other sites More sharing options...
PFMaBiSmAd Posted June 16, 2010 Share Posted June 16, 2010 Just trying various pieces of code and throwing each away without troubleshooting why it does not work rarely leads to a quick solution to a problem because - A) You don't learn why any particular method does not work so that you can identify similar code and avoid using methods that don't have a chance of working (I'm going to guess that you have wasted time trying several logically equivalent pieces of code, all with the same result), and B) You could have a systematic problem (with sessions or your database) that until you identify why any particular code does not work, you can try hundreds of things, but the same unidentified underlying problem will prevent all of them from working. Quote Link to comment https://forums.phpfreaks.com/topic/204905-pulling-information-from-session-id/#findComment-1072736 Share on other sites More sharing options...
dan_t Posted June 16, 2010 Author Share Posted June 16, 2010 That makes sense. I'll alter the line of code with changes from here on. Thanks PFMaBiSmAd Quote Link to comment https://forums.phpfreaks.com/topic/204905-pulling-information-from-session-id/#findComment-1072741 Share on other sites More sharing options...
Soldier Jane Posted June 16, 2010 Share Posted June 16, 2010 I was once advised to use print_r($var) in a situation like this to see if it was a problem with my session vars. Quote Link to comment https://forums.phpfreaks.com/topic/204905-pulling-information-from-session-id/#findComment-1072751 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.