Jump to content

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


Eiolon

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.