Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-20-2010, 11:38 AM   PM User | #1
pphpnewbie
New Coder

 
Join Date: Jan 2010
Posts: 36
Thanks: 1
Thanked 0 Times in 0 Posts
pphpnewbie is an unknown quantity at this point
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
pphpnewbie is offline   Reply With Quote
Old 01-20-2010, 12:15 PM   PM User | #2
ssonawa
New Coder

 
Join Date: Jul 2008
Location: India
Posts: 39
Thanks: 0
Thanked 2 Times in 2 Posts
ssonawa is an unknown quantity at this point
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
ssonawa is offline   Reply With Quote
Old 01-20-2010, 12:19 PM   PM User | #3
kbluhm
Senior Coder

 
kbluhm's Avatar
 
Join Date: Apr 2007
Location: Philadelphia, PA, USA
Posts: 1,502
Thanks: 2
Thanked 258 Times in 254 Posts
kbluhm will become famous soon enough
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.
__________________
ZCE
kbluhm is offline   Reply With Quote
Old 01-20-2010, 12:30 PM   PM User | #4
pphpnewbie
New Coder

 
Join Date: Jan 2010
Posts: 36
Thanks: 1
Thanked 0 Times in 0 Posts
pphpnewbie is an unknown quantity at this point
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
pphpnewbie is offline   Reply With Quote
Old 01-20-2010, 12:42 PM   PM User | #5
kar2905
New Coder

 
Join Date: Jun 2009
Location: Manipal
Posts: 45
Thanks: 2
Thanked 3 Times in 3 Posts
kar2905 is an unknown quantity at this point
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 .
__________________
Kartik Mandaville
http://www.mkartik.com
http://twitter.com/kar2905/
kar2905 is offline   Reply With Quote
Old 01-28-2010, 05:01 AM   PM User | #6
ssonawa
New Coder

 
Join Date: Jul 2008
Location: India
Posts: 39
Thanks: 0
Thanked 2 Times in 2 Posts
ssonawa is an unknown quantity at this point
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.
ssonawa is offline   Reply With Quote
Old 01-28-2010, 05:02 AM   PM User | #7
ssonawa
New Coder

 
Join Date: Jul 2008
Location: India
Posts: 39
Thanks: 0
Thanked 2 Times in 2 Posts
ssonawa is an unknown quantity at this point
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.
ssonawa is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:15 PM.


Advertisement
Log in to turn off these ads.