Passing session variables in a query

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
grantp22
Forum Newbie
Posts: 24
Joined: Tue Sep 15, 2009 9:34 pm

Passing session variables in a query

Post by grantp22 »

Hi

Can somebody please give me the corect syntax for the following query, I am finding it difficult to pass the username of a session in a query. I have tried so many ways to implement this, but it just won't work...

These are some of the methods I have tried! What am I doing wrong. It's in PHP by the way!

Code: Select all

  $q = "SELECT user, message, senttime "
       ."FROM ".TBL_AUCTION_RESPONSES." WHERE user=".$session->username." ORDER BY senttime DESC";
 
   $q = "SELECT user, message, senttime "
       ."FROM ".TBL_AUCTION_RESPONSES." WHERE user='.$session->username.' ORDER BY senttime DESC";
 
   $q = "SELECT user, message, senttime "
       ."FROM ".TBL_AUCTION_RESPONSES." WHERE user='<?php echo $session->username;?>' ORDER BY senttime DESC";

If I use a string in the query as seen below in bolded letters, the query works just fine!


$q = "SELECT user, message, senttime "
."FROM ".TBL_AUCTION_RESPONSES." WHERE user='grant' ORDER BY senttime DESC";


Can somebody tell me what the syntax should be for variables in a case like this

Thanks
Grant
Iainzor
Forum Newbie
Posts: 4
Joined: Mon Oct 26, 2009 10:54 am

Re: Passing session variables in a query

Post by Iainzor »

There are a couple of things wrong with that (looking at the first one since it's the most correct).

1. You probably have to single quote ' the username value:

Code: Select all

WHERE user=[b]'[/b]".$session->username."[b]'[/b]
2. Where are you getting the $session object from? If you are using a session variable, you get those by:

Code: Select all

$_SESSION['username'];
jegan.aaodis
Forum Newbie
Posts: 15
Joined: Fri Oct 09, 2009 1:56 am

Re: Passing session variables in a query

Post by jegan.aaodis »

Hi,

Please start the session first,
session_start();
Then store the value in a variable and check whether the value is passing.
ie $start=$_SESSION[name]; ;

Please try this

Thanks
Post Reply