Jump to content

[SOLVED] Trying the query user info from a session variable


Recommended Posts

Here is what I am trying to do:

 

When a user logs in, a session variable is passed.  The variable is the users ID.  If the variable is passed successfully, they are logged in and I want to query the users information based on the session variable.

 

<?php # main.php

// Start the session.
session_start();

// Check for the session value.
if (isset($_SESSION['id'])) {

// Query database for user information.
$query = "SELECT username, firstname, lastname FROM users WHERE id = '.$_SESSION['id'].'";
$result = mysql_query ($query) OR die ('Cannot execute the query.');
$auth = mysql_fetch_array ($result);

} else {
// Quit the script and redirect to login page.
header ("Location: http://" . $_SERVER['HTTP_HOST'] . 
dirname ($_SERVER['PHP_SELF']) . "index.php");
exit();
}

?>

 

Here is the error I am receiving:

 

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in C:\wamp\www\main.php on line 10

 

I can get this to work if I create and pass a variable for each piece of user info during the login process but I thought it would be easier to pass the users ID and just query their info from there.

 

Thanks for your help!

Replace this;

$query = "SELECT username, firstname, lastname FROM users WHERE id = '.$_SESSION['id'].'";

 

With this;

$query = "SELECT username, firstname, lastname FROM users WHERE id = ".$_SESSION['id']."";

 

That should work. Whenever you are using ' . . ', don't use single quotes( ' ), use double quotes( " ).

 

Hope that works/helps.

 

--

DJ

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.