Help with session.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
barffy
Forum Newbie
Posts: 11
Joined: Mon Aug 30, 2010 6:15 am

Help with session.

Post 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
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Help with session.

Post 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
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Post Reply