By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,067 Members | 1,849 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,067 IT Pros & Developers. It's quick & easy.

Calling session variable inside a function

P: n/a
Related to my previous question on searching through an array, I
discovered the issue. I have a session variable ($username) which I
can not call inside my functions. I can call it just fine outside the
function. Suggestions?

Also, is it an issue because my array returns a column with the same
name?

****************************

function respond_check($qid)
{
include "db.inc.php";
$postchk2 = mysql_query("SELECT username FROM `answers` , `users`
WHERE `qid` = $qid AND `a_uid` = `userid` LIMIT 0,

30",$db);

while ($userx=mysql_fetch_array($postchk2))
{
if (in_array("$username",$userx)) {
echo "Got I finally Got ITT!!!!";
}
}

}
echo "$username is cool"; // This works here but not within the
function

****************************

May 18 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Akhenaten wrote:
Related to my previous question on searching through an array, I
discovered the issue. I have a session variable ($username) which I
can not call inside my functions. I can call it just fine outside the
function. Suggestions?

Also, is it an issue because my array returns a column with the same
name?

****************************

function respond_check($qid)
{
include "db.inc.php";
$postchk2 = mysql_query("SELECT username FROM `answers` , `users`
WHERE `qid` = $qid AND `a_uid` = `userid` LIMIT 0,

30",$db);

while ($userx=mysql_fetch_array($postchk2))
{
if (in_array("$username",$userx)) {
echo "Got I finally Got ITT!!!!";
}
}

}
echo "$username is cool"; // This works here but not within the
function

****************************
Variables external to a function are only valid if:

1) They are superglobals (i.e. $_SESSION, $_GET, $_POST, etc.),
2) They are defined as global in the function, or
3) They are passed to the function.

#3 is the best way to go for user variables; just pass the $username as
a parameter to the function.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
May 18 '07 #2

P: n/a
Variables external to a function are only valid if:

1) They are superglobals (i.e. $_SESSION, $_GET, $_POST, etc.),
2) They are defined as global in the function, or
3) They are passed to the function.

#3 is the best way to go for user variables; just pass the $username as
a parameter to the function.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstuck...@attglobal.net
==================
Jerry, you just saved me from a painful 'death by code' experience.
Thank you.

May 18 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.