Page 1 of 2
PHP not keeping variables
Posted: Thu Oct 13, 2005 1:44 am
by Lostnode
I had created a piece of PHP Code for a client, that basicalt took infor from MySQL, all that works, but PHP is not keeping a variable.
I have this code at the begining
Code: Select all
if( $cntry == '' )
{
echo "Please select a country<br><p>";
}
Which basicaly states, until you define cntry, place this text, this works fine
I then have 2 links, one to index.php?cntry=US and one to index.php?cntry=CA and then the followign
Code: Select all
if ( $cntry )
{
if ( $cntry=='US' )
{
bunch of MySQL code
} else {
bunch of code
}
}
Now what happens is cntry keeps defaulting to blank. SO it never leaves the "Please Select A country" text.
Now if I go in and define:
I get all the Candian stats, and is I mark
I get all the US Stats... but I wanted it to be dynamic, and it was, till I had to reformat my system... Now PHP Won't keep the efina variable... I use index.php?cntry=US or CA and it still asks me for the country.
does this have to do with magic quotes at all?
Posted: Thu Oct 13, 2005 1:56 am
by jurriemcflurrie
try
Code: Select all
if ( $_GET['cntry'] )
{
if ( $_GET['cntry']=='US' )
{
bunch of MySQL code
} else {
bunch of code
}
}
Posted: Thu Oct 13, 2005 3:24 pm
by Lostnode
That doesnt work either.
SInce I cannot post attachements, I will post the whole code and hope it fits.
Code: Select all
<!doctype html public "-//W3C//DTD HTML 4.0 //EN">
<html>
<head>
<title>Distrobution</title>
<link href="/umm.css" rel="stylesheet" type="text/css">
</head>
<body>
<?php
//Declare what happens is there is no Country Selected.
if ( $cntry == '' )
{
echo "Please select a country<br><p>";
}
//Declair valused for Order and Sort if none exist
$order = 'age';
$ascdesc = 'ASC';
//Place Country Selection at top
echo "<a href='?cntry=US'>U.S. distribution channels</a> - <a href='?cntry=CA'>Canadian distribution channels</a>\n";
// Connecting, selecting database
$link = mysql_connect('localhost', 'username', 'password') // Username and Password removed for security purposes
or die('Could not connect: ' . mysql_error());
mysql_select_db('database') or die('Could not select database'); // Database name removed for security purposes
// Printing results in HTML
echo "<table bgcolor='#CCCCCC' width=600>\n";
//Header
if ( $cntry )
{
if ( $cntry == 'US' )
{
echo "<tr><td bgcolor='#000000'><font color='#FFFFFF'><b> U.S. Channels </b></font></td><td bgcolor='#000000'><font color='#FFFFFF'><b> Age </b></font></td><td bgcolor='#000000'><font color='#FFFFFF'><b> Locations </b></font></td>";
echo "<td bgcolor='#000000'><font color='#FFFFFF'><b> Reach </b></font></td><td bgcolor='#000000'><font color='#FFFFFF'><b> Timing </b></font></td><td bgcolor='#000000'><font color='#FFFFFF'><b> Gender </b></font></td></tr>\n";
} else {
echo "<tr><td bgcolor='#000000'><font color='#FFFFFF'><b> Canadian Channels </b></font></td><td bgcolor='#000000'><font color='#FFFFFF'><b> Age </b></font></td><td bgcolor='#000000'><font color='#FFFFFF'><b> Locations </b></font></b></font></td>";
echo "<td bgcolor='#000000'><font color='#FFFFFF'><b> Reach </b></font></td><td bgcolor='#000000'><font color='#FFFFFF'><b> Timing </b></font></td><td bgcolor='#000000'><font color='#FFFFFF'><b> Gender </b></font></td></tr>\n";
}
}
if ( $cntry )
{
// Students
// Performing SQL query
$query = "SELECT channel,age,locations,reach,timing,gender FROM distribution WHERE cat = '1' && country = '$cntry' ORDER BY $order $ascdesc";
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
echo "<tr><td colspan=7 bgcolor='#404041'><font color='#FFFFFF'><b> Students </b></font></td></tr>\n";
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo "\t<tr>\n";
foreach ($line as $col_value) {
echo "\t\t<td bgcolor='#FFFFFF'>$col_value</td>\n";
}
echo "\t</tr>\n";
}
// Free resultset
mysql_free_result($result);
//Fitness / Sporting
// Performing SQL query
$query = "SELECT channel,age,locations,reach,timing,gender FROM distribution WHERE cat = '2' && country = '$cntry' ORDER BY $order $ascdesc";
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
echo "<tr><td colspan=7 bgcolor='#404041'><font color='#FFFFFF'><b> Fitness / sporting </b></font></td></tr>\n";
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo "\t<tr>\n";
foreach ($line as $col_value) {
echo "\t\t<td bgcolor='#FFFFFF'>$col_value</td>\n";
}
echo "\t</tr>\n";
}
// Free resultset
mysql_free_result($result);
//Resort/Hotel/Camping
// Performing SQL query
$query = "SELECT channel,age,locations,reach,timing,gender FROM distribution WHERE cat = '3' && country = '$cntry' ORDER BY $order $ascdesc";
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
// Printing results in HTML
echo "<tr><td colspan=7 bgcolor='#404041'><font color='#FFFFFF'><b> Resort / hotel / camping </b></font></td></tr>\n";
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo "\t<tr>\n";
foreach ($line as $col_value) {
echo "\t\t<td bgcolor='#FFFFFF'>$col_value</td>\n";
}
echo "\t</tr>\n";
}
// Free resultset
mysql_free_result($result);
//Community
// Performing SQL query
$query = "SELECT channel,age,locations,reach,timing,gender FROM distribution WHERE cat = '4' && country = '$cntry' ORDER BY $order $ascdesc";
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
// Printing results in HTML
echo "<tr><td colspan=7 bgcolor='#404041'><font color='#FFFFFF'><b> Community </b></font></td></tr>\n";
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo "\t<tr>\n";
foreach ($line as $col_value) {
echo "\t\t<td bgcolor='#FFFFFF'>$col_value</td>\n";
}
echo "\t</tr>\n";
}
// Free resultset
mysql_free_result($result);
// Entertainment
// Performing SQL query
$query = "SELECT channel,age,locations,reach,timing,gender FROM distribution WHERE cat = '5' && country = '$cntry' ORDER BY $order $ascdesc";
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
// Printing results in HTML
echo "<tr><td colspan=7 bgcolor='#404041'><font color='#FFFFFF'><b> Entertainment </b></font></td></tr>\n";
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo "\t<tr>\n";
foreach ($line as $col_value) {
echo "\t\t<td bgcolor='#FFFFFF'>$col_value</td>\n";
}
echo "\t</tr>\n";
}
// Free resultset
mysql_free_result($result);
//Retail/Commercial
// Performing SQL query
$query = "SELECT channel,age,locations,reach,timing,gender FROM distribution WHERE cat = '6' && country = '$cntry' ORDER BY $order $ascdesc";
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
// Printing results in HTML
echo "<tr><td colspan=7 bgcolor='#404041'><font color='#FFFFFF'><b> Retail / commercial </b></font></td></tr>\n";
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo "\t<tr>\n";
foreach ($line as $col_value) {
echo "\t\t<td bgcolor='#FFFFFF'>$col_value</td>\n";
}
echo "\t</tr>\n";
}
// Free resultset
mysql_free_result($result);
//Personal/Medical Care
// Performing SQL query
$query = "SELECT channel,age,locations,reach,timing,gender FROM distribution WHERE cat = '7' && country = '$cntry' ORDER BY $order $ascdesc";
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
// Printing results in HTML
echo "<tr><td colspan=7 bgcolor='#404041'><font color='#FFFFFF'><b> Personal / medical care </b></font></td></tr>\n";
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo "\t<tr>\n";
foreach ($line as $col_value) {
echo "\t\t<td bgcolor='#FFFFFF'>$col_value</td>\n";
}
echo "\t</tr>\n";
}
echo "</table>\n";
// Free resultset
mysql_free_result($result);
}
// Closing connection
mysql_close($link);
?>
</body>
</html>
Like I said, this exact document used to work just fine till I reformated my system, which means it could possible be a setting in PHP. If I assign a value to $cntry I get the right infor, but I cannot get ti to work at all like this.
Posted: Thu Oct 13, 2005 3:29 pm
by Burrito
sounds like ye ole' register globals prob...
search this forum, you'll find plenty.
I'd keep them off though and just recode your page

Posted: Thu Oct 13, 2005 3:34 pm
by Lostnode
When I use $_Get['cntry'] I get this error:
Code: Select all
Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/un/public_html/channels/index2.php on line 52
Line 52, and every otehr MySQL Query looks like this:
Code: Select all
$query = "SELECT channel,age,locations,reach,timing,gender FROM distribution WHERE cat = '1' && country = $_GET['cntry'] ORDER BY $order $ascdesc";
It doesn;t like the $_GET command.
Posted: Thu Oct 13, 2005 3:47 pm
by Burrito
GET needs to be all caps:
ie:
Posted: Thu Oct 13, 2005 3:49 pm
by Lostnode
Burrito wrote:GET needs to be all caps:
ie:
It was in my code (llok at the code, not what I wrote by hand)
It as the register globals, but how do I gode it with the $_GET? It wasn;t working at all.
Posted: Thu Oct 13, 2005 3:52 pm
by Burrito
for the sake of ease you might want to just set $ctnry to $_GET['cntry'], then you wont' have to change anything else.
Posted: Thu Oct 13, 2005 4:23 pm
by Lostnode
Burrito wrote:for the sake of ease you might want to just set $ctnry to $_GET['cntry'], then you wont' have to change anything else.
You mean
?
Posted: Thu Oct 13, 2005 4:28 pm
by Burrito
no just a single equal....you're not comparing you're setting
Posted: Thu Oct 13, 2005 4:30 pm
by Lostnode
Burrito wrote:no just a single equal....you're not comparing you're setting
You must have a very hires screen Burrito... It is a double equal sign...

Thank you for the help, I will see what I can do about it.
Posted: Thu Oct 13, 2005 4:31 pm
by Charles256
he meant.
no, use a single equal instead of a double equal. you want to set it equal to that, not compare the two.
Posted: Thu Oct 13, 2005 4:33 pm
by Lostnode
Ah, my appologies... Misread it... Ok, I will use a single quote. Thnx
Posted: Thu Oct 13, 2005 4:42 pm
by Lostnode
Ok, didn;t work... I still get this error when ever I use $_GET
Code: Select all
Parse error: parse error, unexpected T_IF in /home/un/public_html/channels/index.php on line 12
Code: Select all
$cntry ="$_GET['cntry']"
if ( $cntry == '' ) //This is line 12, and its 2 x ' not "
{
echo "Please select a country<br><p>";
}
even tried
Code: Select all
$cntry = $_GET['cntry']
if ( $cntry == '' ) //This is line 12, and its 2 x ' not "
{
echo "Please select a country<br><p>";
}
Posted: Thu Oct 13, 2005 4:44 pm
by Burrito
lose the quotes
Code: Select all
$cntry = $_GET['cntry'];
if ( $cntry == '' ) //This is line 12, and its 2 x ' not "
{
echo "Please select a country<br><p>";
}
damn it, either you edited or I missed it
anyway, try print_r'ing your $_GET[] array to see what it shows...