Passing session variables in a query

Discussion in 'Databases' started by grantp22, Oct 27, 2009.

  1. #1
    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!

       $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";
    Code (markup):

    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
     
    grantp22, Oct 27, 2009 IP
  2. youngone324

    youngone324 Peon

    Messages:
    125
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    $q = mysql_query("SELECT user, message, senttime "
    ."FROM ".TBL_AUCTION_RESPONSES." WHERE user='grant' ORDER BY senttime DESC");
     
    youngone324, Oct 27, 2009 IP
  3. grantp22

    grantp22 Peon

    Messages:
    15
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    LightGraphicz

    I know how to query the data base, I am using a seperate database php file that handels the actual queries ie: mysql_query

    I am having difficulty with the syntax for including a session variable ($session->username) into the query string which gets passed to database.php

       global $database;
       $q = "SELECT user, message, senttime "
           ."FROM ".TBL_AUCTION_RESPONSES." WHERE user="" & $session->username & "" ORDER BY senttime DESC";
       $result = $database->query($q);
    
    Code (markup):
    Thanks for the quick response
     
    grantp22, Oct 27, 2009 IP
  4. youngone324

    youngone324 Peon

    Messages:
    125
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    global $database;
    $q = "SELECT user, message, senttime "
    ."FROM ".TBL_AUCTION_RESPONSES." WHERE user="" & $session>=username & "" ORDER BY senttime DESC";
    $result = $database->query($q);



    you want it to be greater then or equal to or just greater then?
     
    youngone324, Oct 27, 2009 IP
  5. grantp22

    grantp22 Peon

    Messages:
    15
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    The code I suplied in my second post is not correct I was just trying out the double quotes, well actually I've been trying just about everything except the correct syntax
     
    grantp22, Oct 27, 2009 IP
  6. grantp22

    grantp22 Peon

    Messages:
    15
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I am trying to pass the session user name in the query string but can't find the correct syntax or structure it should be in to work


    $q = "SELECT user, message, senttime "
        . "FROM " . TBL_AUCTION_RESPONSES . " WHERE user=" . $_SESSION['username'] . " ORDER BY senttime DESC";
    Code (markup):
    I am not trying to do greater than or anything like that I have just been trying different way of structuring the queries, the username is part of a session array
     
    grantp22, Oct 27, 2009 IP
  7. kimmy1986

    kimmy1986 Peon

    Messages:
    45
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Why dont you make a string variable using session variable for query and then execute it
     
    kimmy1986, Oct 28, 2009 IP