Jump to content

Queries using session variables


mwill101

Recommended Posts

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);

?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

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

Guest
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.