Error in my SESSION code

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
gimpact
Forum Commoner
Posts: 65
Joined: Tue Jun 16, 2009 11:08 pm

Error in my SESSION code

Post by gimpact »

Hello,

I am new to php. I am designing a simple login form, which up on login retrieves few words from the database and displays to the user using SESSION. Unfortunately, my code isn't displaying any thing. All it displays is '0'.

Database structure:

Code: Select all

single_word(
wordID integer not null auto_increment,
word char(60)
primary key (wordID));
 
game (
emailID Char(60) 
userword Char(32),
usertext text,
PRIMARY KEY (postID),
FOREIGN KEY (userword) REFERENCES single_word(word));
 
userdata (
  name varchar(30) DEFAULT NULL,
  email varchar(60) NOT NULL,
  activationpassword float DEFAULT NULL,
  password text,
  validation char(20) 
  banned char(20) 
  PRIMARY KEY (email)
) 
 
Once all criteria for login is successfully executed I am using following code to implement session

Code: Select all

session_start();
                        $_SESSION['email']=$email;
                        if(isset($_SESSION['email'])){
                            // Oneword
                            $oneword = "SELECT sw.word FROM single_word sw WHERE sw.word NOT IN (SELECT g.userword FROM game g WHERE g.emailID='$email') limit 1";
                            $result = mysql_query($oneword) or die ("Ahh!Could not get from database");
                            
                            $description = "SELECT description FROM single_word WHERE word ='$result'";
                            $description = mysql_query($description) or die ("Ahh! could not get DESCRIPTION from database");
 
                            $_SESSION['oneword'] = $oneword;
                            $_SESSION['description'] = $description;
                            mysql_close($con);
and this is how i am displaying in display.php

Code: Select all

<?php
if(isset($_SESSION['email'])){
    $description = $_SESSION['description'];
    $oneword = $_SESSION['oneword'];
}
?>
<?php print $description ?>
<?php print $oneword ?>
Any help will be appreciated.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Error in my SESSION code

Post by requinix »

mysql_query only runs the query - it doesn't give you the results back. You have to use a function like mysql_fetch_assoc to do that.
And you use your variables inconsistently: $oneword for SQL and $result for the result, the $description for both SQL and result.
And you could do your query smarter. And I don't see a "description" in the single_word table.

Code: Select all

$sql = "SELECT sw.word, sw.description FROM single_word sw WHERE sw.word NOT IN (SELECT g.userword FROM game g WHERE g.emailID = '$email') LIMIT 1";
$result = mysql_query($sql) or die("Ahh! Could not get from database");
$row = mysql_fetch_assoc($result);
$_SESSION["oneword"] = $row["oneword"];
$_SESSION["description"] = $row["description"];
Post Reply