Problem assigning a query result to a $_SESSION variable

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
usboy2903
Forum Newbie
Posts: 1
Joined: Sun Nov 20, 2011 4:31 pm

Problem assigning a query result to a $_SESSION variable

Post by usboy2903 »

Hi, well im working on a project for school but I can't seem to get it to work.

Its an app that is connected to a DB with 3 tables, this is my sql syntax of the db:

SQL SYNTAX

Table structure for table `income`

CREATE TABLE `income` (
`idincome` int(11) NOT NULL AUTO_INCREMENT,
`DoI` date NOT NULL,
`Quantity` int(11) NOT NULL,
`Type` varchar(45) COLLATE latin1_general_ci NOT NULL,
`Comment` varchar(45) COLLATE latin1_general_ci NOT NULL,
`iduser` int(11) NOT NULL,
PRIMARY KEY (`idincome`),
KEY `iduser` (`iduser`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci AUTO_INCREMENT=1;

Table structure for table `outcome`

CREATE TABLE `outcome` (
`idoutcome` int(11) NOT NULL AUTO_INCREMENT,
`DoO` date NOT NULL,
`Quantity` int(11) NOT NULL,
`Type` varchar(45) COLLATE latin1_general_ci NOT NULL,
`Comment` varchar(45) COLLATE latin1_general_ci NOT NULL,
`iduser` int(11) NOT NULL,
PRIMARY KEY (`idoutcome`),
KEY `iduser` (`iduser`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci AUTO_INCREMENT=1 ;

Table structure for table `user`CREATE TABLE `user` (
`iduser` int(11) NOT NULL AUTO_INCREMENT,
`FirstName` varchar(45) COLLATE latin1_general_ci NOT NULL,
`LastName` varchar(45) COLLATE latin1_general_ci NOT NULL,
`Email` varchar(45) COLLATE latin1_general_ci NOT NULL,
`DoB` date NOT NULL,
`Sex` varchar(6) COLLATE latin1_general_ci NOT NULL,
`Username` varchar(45) COLLATE latin1_general_ci NOT NULL,
`Password` varchar(45) COLLATE latin1_general_ci NOT NULL,
PRIMARY KEY (`iduser`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci AUTO_INCREMENT=27 ;



So basically, I use a "iduser" to link the user currently in the session with the income and outcome tables.

What I'm trying to do is that when the client logs in, I select the iduser of that person and saving it in a session variable. This is the code:


Code: Select all

    <?php
     
    session_start();
     
    //Connect to the DB
    $host = 'HOST';
    $user = 'USER';
    $pass = 'PASS';
    $db = 'DB';
    mysql_connect($host, $user, $pass) or die("Could not connect: ".mysql_error());
    mysql_select_db($db) or die(mysql_error());
     
    //Grab username and password that the client typed in.
    $username = $_POST['Username'];
    $password = md5($_POST['Password']);
     
    //Query to grab the iduser of that user.
    $queryid = "SELECT iduser FROM user WHERE Username = '$username'";
    $id = mysql_query($queryid);
    $_SESSION['id'] = $id;
     
     
    //Query to check if the username and password are in the db.
    $query = "SELECT * FROM user WHERE Username = '$username' AND Password= '$password'";
    $result = mysql_query($query);
     
     
    //Check if the user is in the db
    if (mysql_num_rows($result) != 1)
    {
    //If he's not, send to loginfail.php
    header('location:loginfail.php');
    }else
    {
    //If he is, take him to home.php and save a SESSION variable with his username.
    $_SESSION['username'] = $username;
    header( 'location: home.php');
    }
    ?>

[/size]

So when he/she logs in successfully, they're taken to home.php and I display both of the session variables, BUT when I try to display the $_SESSION['id'] variable, I get a 0 instead of the users unique id.

That's my problem and I really don't know how to solve it :/
I could really use some help with this..

Thanks!
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Problem assigning a query result to a $_SESSION variable

Post by Celauran »

Code: Select all

//Grab username and password that the client typed in.
$username = $_POST['Username'];
$password = md5($_POST['Password']);
Don't use md5 for passwords.

Code: Select all

//Query to grab the iduser of that user.
$queryid = "SELECT iduser FROM user WHERE Username = '$username'";
$id = mysql_query($queryid);
$_SESSION['id'] = $id;
$id is a mysql_result, which is why you're running into trouble. Try this:

Code: Select all

//Query to grab the iduser of that user.
$queryid = "SELECT iduser FROM user WHERE Username = '$username'";
list($id) = mysql_fetch_row(mysql_query($queryid));
$_SESSION['id'] = $id;

Code: Select all

//Query to check if the username and password are in the db.
$query = "SELECT * FROM user WHERE Username = '$username' AND Password= '$password'";
$result = mysql_query($query);
You just ran this query. Why are you running it again?
Post Reply