Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Cannot understand sessions

Status
Not open for further replies.

rcubed

IS-IT--Management
Joined
Feb 10, 2004
Messages
7
Location
US
Can someone please help me understand why I cannot query the database using a session variable? The code below will work properly if I change the $logname to a value that I know exist in the database, but when using the session variable $logname,,, it does not appear to work. The session variable $logname is set and I can see it on the echo command on the 4th line below. I must be missing something or not understanding how the sessions work. Please help!

<?php
session_start();
include(&quot;rcubed.inc&quot;);
echo $logname;

$connection= mysql_connect($host, $user, $password) or die (&quot;Couldn't connect to server&quot;);
$db = mysql_select_db($database, $connection) or die (&quot;Couldn't select database&quot;);

function build_dropdown() {
$sql = &quot;select customer_id from customer where loginname='$logname'&quot;;
$result=mysql_query($sql) or die (mysql_error().&quot; - &quot;.mysql_errno());

while($row=mysql_fetch_array($result, MYSQL_ASSOC))
{
extract($row);
$output .= &quot;<OPTION VALUE=$customer_id>$customer_id</OPTION>&quot;;
}
return $output;
}
?>

<html>

<head>
<meta http-equiv=&quot;Content-Language&quot; content=&quot;en-us&quot;>
<meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=windows-1252&quot;>
<meta name=&quot;GENERATOR&quot; content=&quot;Microsoft FrontPage 5.0&quot;>
<meta name=&quot;ProgId&quot; content=&quot;FrontPage.Editor.Document&quot;>
<title>Example form using JavaScript to change information</title>

</head>

<body>


<form name=&quot;globe&quot; method=&quot;POST&quot; action=&quot;test_submit_rma.php&quot;>
<p align=&quot;center&quot;></p>
<p align=&quot;center&quot;>&nbsp;</p>
<p align=&quot;center&quot;>&nbsp;</p>
<p align=&quot;center&quot;>&nbsp;</p>
<p align=&quot;center&quot;>Ship to State</p>
<p></p>
<p align=&quot;center&quot;><select name=&quot;scountry&quot;>
<?php echo build_dropdown() ?>
</select>

</form>

</body>

</html>

 
I am just getting started with php, so I'm not sure.. How would I display $sql
 
print $sql; at that point displays nothing... I am guessing because the function only returns the output...
 
It seems as if the session variable $logname is not available at the time of the query but it is available on the 4th line when it is echo....Why would I be able to see the variable at first but not when I run the function build_dropdown?
 
Thanks.. never thought about looking there.. the following is what is shown in source:

select customer_id from customer where loginname= ''

Why is the $logname empty?
 
I see why. It has to do with variable scope.

A function has its own variable scope, so inside the function $logname refers to a variable, separate but with the same name, that exists only inside the function.

You have several workarounds.

One, if you are running PHP version >= 4.1.0, is to reference PHP's superglobal arrays directly:

$sql = &quot;select customer_id from customer where loginname='&quot; . $_SESSION['logname'] . &quot;'&quot;;

PHP's superglobal arrays are documented here:
. Your code, when it expects a session variable to be instantiated as $logname, needs the PHP configuration directive register_globals to be set to on. This has security implications discussed here:

Another way is to use the &quot;global&quot; operator to tell PHP to use the variable from the main program scope. &quot;global&quot; is documented here:
Want the best answers? Ask the best questions: TANSTAAFL!!
 
dude.. &quot;You are the Man&quot;
Thank you very much!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top