mwill101 Posted January 20, 2012 Share Posted January 20, 2012 Hello! I'm a bit of a novice on PHP variables still and perhaps I'm trying to run before I can walk. I have an SQL database that I am able to successfully write to without any problems. I have also created a session variable for the user id number that I am able to retrieve on a later page. My problem is that I have hit a wall when it comes to retrieving the data from the database based upon the session variable user id. Here is my code and error: $_SESSION['ID_ASSIGNED']=$_POST[KSU_ID]; //this line takes the form data and successfully assigns it to the session variable on an earlier page echo $_SESSION['ID_ASSIGNED']; //that line works normally. Here's my problem code (the line starting with $result ). The line works fine if I input: $result = mysql_query("SELECT * FROM $usertable WHERE KSU_ID='some id number'");: <?php $result = mysql_query("SELECT * FROM $usertable WHERE KSU_ID=$_SESSION['ID_ASSIGNED']"); $row = mysql_fetch_array($result); echo "<br />Participant Number:"; echo $row['ID']; echo "<br />Name: "; echo $row['lname']; echo ", "; echo $row['fname']; echo "<br />Student Number:"; echo $row['KSU_ID']; echo "<br />Age: "; echo $row['age']; echo "<br />Gender: "; echo $row['gender']; echo "<br />Grade: "; echo $row['grade']; mysql_close($con); ?> Quote Link to comment https://forums.phpfreaks.com/topic/255443-queries-using-session-variables/ Share on other sites More sharing options...
mwill101 Posted January 20, 2012 Author Share Posted January 20, 2012 Solved my problem. The variable needed to be assigned by using the following line: $result = mysql_query("SELECT * FROM $usertable WHERE KSU_ID='".$_SESSION['ID_ASSIGNED']."'"); Essentially, $_SESSION['ID_ASSIGNED'] needed to be written as '".$_SESSION['ID_ASSIGNED']."' Don't quite get the reason for that formatting but I can roll with it. Quote Link to comment https://forums.phpfreaks.com/topic/255443-queries-using-session-variables/#findComment-1309669 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.