Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Jan 2010
    Posts
    36
    Thanks
    1
    Thanked 0 Times in 0 Posts

    How to make use of session viarables

    Would like to know how to make use of the session variables to fetch data from the database.

    Code:
    $colname_getName = "-1";
    if (isset($_SESSION['MM_Username'])) {
      $colname_getName = $_SESSION['MM_Username'];
    }
    mysql_select_db($database_connAdmin, $connAdmin);
    $query_getName = sprintf("SELECT user_id, surname, lastname FROM users WHERE username = %s", GetSQLValueString($colname_getName, "text"));
    $getName = mysql_query($query_getName, $connAdmin) or die(mysql_error());
    $row_getName = mysql_fetch_assoc($getName);
    $totalRows_getName = mysql_num_rows($getName);
    $_SESSION['user_id'] = $row_getName['user_id'];
    $_SESSION['lastname'] = $row_getName['lastname'];
    $_SESSION['surname'] = $row_getName['surname'];
    I want to use $_SESSION['user_id'] to get data from another table that has user_id as foreign key. For the last 2 days I can't manage to figure out how.

    TIA

  • #2
    New Coder
    Join Date
    Jul 2008
    Location
    India
    Posts
    39
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Thumbs up

    Do you want to use $_SESSION['user_id'] (which is saved in your above code) to query to a different table? If yes then you can simply us it in a query as below -

    $query_getName_1 = sprintf("SELECT user_id, address FROM users_address WHERE user_id= $_SESSION['user_id']" );


    Let me know if you have other issue.

    Cheers,
    Ssonawa

  • #3
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    Quote Originally Posted by ssonawa View Post
    Do you want to use $_SESSION['user_id'] (which is saved in your above code) to query to a different table? If yes then you can simply us it in a query as below -

    $query_getName_1 = sprintf("SELECT user_id, address FROM users_address WHERE user_id= $_SESSION['user_id']" );


    Let me know if you have other issue.

    Cheers,
    Ssonawa
    You're using sprintf() incorrectly and unnecessarily... and that string will also send an incorrectly formatted query. You need to concatenate the variable, or wrap it in curly braces, since you are including the key value.

  • #4
    New Coder
    Join Date
    Jan 2010
    Posts
    36
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by ssonawa View Post
    Do you want to use $_SESSION['user_id'] (which is saved in your above code) to query to a different table? If yes then you can simply us it in a query as below -

    $query_getName_1 = sprintf("SELECT user_id, address FROM users_address WHERE user_id= $_SESSION['user_id']" );


    Let me know if you have other issue.

    Cheers,
    Ssonawa
    Thanks. What I want to achieve is when John logged in his username will be assigned with this variable

    PHP Code:
        $_SESSION['MM_Username'] = $loginUsername
    and I can fetch data from the user_table. that has John's user_id, username, First Name, Last Name.

    But I cannot figure out how to fetch data from another table that has John's user_id as foreign key.

    If John logged in he will see his personal details and when Tom logged in Tom will see his personal details.

    TIA

  • #5
    New Coder
    Join Date
    Jun 2009
    Location
    Manipal
    Posts
    45
    Thanks
    2
    Thanked 3 Times in 3 Posts
    What exactly do you want to achieve ?

    Do you want the following ?

    As soon as John logs in, the $_SESSION['MM_Username'] stores his userid .
    If its the case, then assign the value from the $_POST[] variable to $_SESSION['MM_Username'] in the page where you check for form submission .

  • #6
    New Coder
    Join Date
    Jul 2008
    Location
    India
    Posts
    39
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Thumbs up

    Quote Originally Posted by pphpnewbie View Post
    Thanks. What I want to achieve is when John logged in his username will be assigned with this variable

    PHP Code:
        $_SESSION['MM_Username'] = $loginUsername
    and I can fetch data from the user_table. that has John's user_id, username, First Name, Last Name.

    But I cannot figure out how to fetch data from another table that has John's user_id as foreign key.

    If John logged in he will see his personal details and when Tom logged in Tom will see his personal details.

    TIA
    Okay.

    After executing above query you will get row contents of the user table for user John. This array can be traversed and get the user_id from it. Store the userid in session and then select personal_detail table, query with where user_id = $user_id_stored.

    Let me know if this gets solved.

  • #7
    New Coder
    Join Date
    Jul 2008
    Location
    India
    Posts
    39
    Thanks
    0
    Thanked 2 Times in 2 Posts
    Quote Originally Posted by kbluhm View Post
    You're using sprintf() incorrectly and unnecessarily... and that string will also send an incorrectly formatted query. You need to concatenate the variable, or wrap it in curly braces, since you are including the key value.
    Oh thanks, I missed that.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •