Page 1 of 1

Help with session.

Posted: Thu Jan 27, 2011 7:51 am
by barffy
Hi,

I have created a log in script that starts a session when the username and password are correct. This functions correctly but in the DB i am storing a value called EST_ID. This is the establishment id for each user. When performing queries from the site, i will use the where clause, so that the data returned corresponds only to people who are a member of the same establishment.

i.e select * from rooms where est_id = $est_id.

To do this, i am trying to return the establishment id from the database for the logged in user, and store the id as a session variable. I am a bit stuck as to how to do this. so far i have got:

Code: Select all

<?php

$query  = "SELECT est_id FROM users where username = '$user'";
$result = mysql_query($query);

while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
    echo "establishment id  :{$row['est_id']} <br>";
} 



session_start();
$_SESSION['est_id'] = "$result";

$myest = $_SESSION["est_id"];
echo "$myest";

?>
When i try to echo the value stored in the session i get: Resource id #5

Can anybody shed any light on how to achieve this?

Thanks in Advance

Harry

Re: Help with session.

Posted: Thu Jan 27, 2011 11:01 am
by social_experiment

Code: Select all

<?php
$result = mysql_query($query);
//
$_SESSION['est_id'] = "$result";
?>
You are storing the wrong value in the session variable, you should store $row['est_id'] instead.

Code: Select all

<?php
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
    echo "establishment id  :{$row['est_id']} <br>";
    $est_id = $row['est_id'];
} 

session_start();
$_SESSION['est_id'] = $est_id;
?>
Hth