...

View Full Version : How to make use of session viarables



pphpnewbie
01-20-2010, 11:38 AM
Would like to know how to make use of the session variables to fetch data from the database.


$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

ssonawa
01-20-2010, 12:15 PM
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

kbluhm
01-20-2010, 12:19 PM
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.

pphpnewbie
01-20-2010, 12:30 PM
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


$_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

kar2905
01-20-2010, 12:42 PM
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 .

ssonawa
01-28-2010, 05:01 AM
Thanks. What I want to achieve is when John logged in his username will be assigned with this variable


$_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
01-28-2010, 05:02 AM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum