Variables magically being unset!
Posted by: IanS (---.prem.tmns.net.au)
Date: September 05, 2006 04:50AM

I am running Wamp5 V1.4.4

Im relativley new to PHP/MySQL etc.

I am getting what i consider wierd issues with
variables being cleared.

***** Test.php ***
include ("./sqlfunctions.php"winking smiley

// have code that sets up a variable called $statment.
$statment = "select * from debtors"
$res = sql($statment) // user function call

**** sqlfunctions.php ****

function sql($statment,$assoc=1)
{
echo $statment // correctly gives me select * from debtors

// connect to DB

If (!mysql_connect(host,user,pass))
{
echo "error" . mysql_error();
exit;
}
echo $statment // this now gives me nothing - variable contents seemed to
// have been cleared??
// when i try to use $statment in actual qry - i get "empty qry" error.
.
.
.
}



So - any obvious mistakes here??
on the surface, it look like calling mysql_connect unsets my variable??!!
Or is this something to do with .ini settings or something?

some wierd vagery of my WAMP5 version?


If i use the EXACT same mysql code inline instead of using a function call,
it all seems to work as expected.

Thing that annoys me is that im told this style of coding works on
someones production LAMP environment!!


regards

Ian.

Options: ReplyQuote
Re: Variables magically being unset!
Posted by: CyberSpatium (67.170.181.---)
Date: September 05, 2006 10:18PM

well, one thing I notice right off the bat is you do not have a mysql_select_db statement. you need to use this so your php script knows which database to use.

[php.net]

Options: ReplyQuote
Re: Variables magically being unset!
Posted by: IanS (---.prem.tmns.net.au)
Date: September 06, 2006 02:54AM

Sorry,

The code i provided doesnt show the entire code - just what i thought were the relavent
bits.

** php.ini **
display_errors = On
error_reporting = E_ALL $ ~E_NOTICE

2. more Code

** test.php **
<?PHP
include ("./sqlfunctions.php"winking smiley; // contains user function called sql, for mysql functions

$selected_id='1';
$statment = "select * from debtors where Ourref=$selected_id";

$res = sql($statment);
$row = mysql_fetch_array($res);

// other code
.
.
?>

** sqlfunctions.php **
<?PHP

function sql($statment,$assoc=1)
{
$dbhost= "host";
$dbuser= "user";
$dbpass= "pass";
$dbname= "dbase";

echo $statment; // displays correct contents of $statment eg: "select * from .... " etc.

if(!mysql_connect($dbhost,$dbuser,$dbpass))
{
echo Stylesheet() . "\n\n<div class=Error>";
echo $Translation["error:"] . mysql_error();
echo "</div>";
exit;
}
echo $statment; // display empty (or null?) contents of $statment eg: "".
// other code.
.
.
.
?>

Summary
=======

The first echo displays the correct contents.
The second echo shows nothing.

The sql statment mysql_connect runs and connects.
The
if(!mysql_connect(xxx))
{
failure code
}
failure code isnt actioned, therefore no other functions or actions are taken before the
second echo $statment;

To me (limited expertise excepted), The $statment variable seems to be reset
by the mysql_connect function call.

I've even used other variable names eg:$statmentxc and get the same result.

This is sending me bonkers!!

There has to be a simple solution to this.

Regards

Ian.

Options: ReplyQuote
Re: Variables magically being unset!
Posted by: CyberSpatium (67.170.181.---)
Date: September 06, 2006 03:46PM

you state that the second echo statement is empty. this is because you use exit() in your code:

if(!mysql_connect($dbhost,$dbuser,$dbpass))
{
echo Stylesheet() . "\n\n<div class=Error>";
echo $Translation["error:"] . mysql_error();
echo "</div>";
exit;
}

your mysql_connect code is not working, so it is using your custom error code above. when you use exit() in your code, it terminates your current script. since your script has now terminated, your second echo statement did not execute because your script has been stopped before that statement was even run. remove that exit() statement and your will see your second echo statement.

More Info:
[php.net]

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

also, you need to use a while loop to loop though your mysql_fetch_array data, so edit this code here:
$row = mysql_fetch_array($res);

More Info:
[php.net]
[php.net]
[php.net]

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

also, it is a good coding practice to use variables correctly. you are usign this
$selected_id='1';

the way you are using this, you are stating that the $selected_id variable is a string with a value of 1, not an integer (number) 1 like you are trying to use it. change you code to
$selected_id=1;

More Info:
[php.net]
[php.net]



Post Edited (09-06-06 15:56)

CyberSpatium
----------------------
WAMP Forum Admin

Web Development for Newbie's Blog - Check out my new blog. It is for web developers, and especially tailored for the web development newbie. If you are not fluent in “geek speak”, then this incredible resource is just you. And even if you are a web development pro, this is a great resource to check out some of the latest web development tips, news, tutorials, codes and more.

Options: ReplyQuote
Re: Variables magically being unset!
Posted by: IanS (---.prem.tmns.net.au)
Date: September 07, 2006 01:27AM

Thks for the detailed responses.

Unfortunately, i dont believe the exit statement is the problem.

I dont get any details that the connect failed.
As i said, if i put the same code inline in a single php file, it all works.

Also, i have put in an echo "hello world"; in the failure code - and i get nothing.

In fact, if i reset the $statment variable just before the eventual mysql_fetch (not shown
in my example code) as ive seen in most tutorial examples , the whole thing works fine.

Its just that the contents of $statment seem to go missing half way thru the code.
As soon as i do the MySQL_connect function.

At this stage, im still thinking my coding logic is ok, and that its a vagary of
Windows, WAMP5, PHP.INI, ???, etc

****

Thks for the note about the variable typing - changed as recommended.

*****
NB: I dont go thru a loop, as the $selected_id field is a auto-increment primary
key field. So pretty sure i'm only going to get the one single value.

Thks for the note though - im still pretty rough at this, so it all helps.


*****


Regards


Ian.

Options: ReplyQuote
Re: Variables magically being unset!
Posted by: CyberSpatium (67.170.181.---)
Date: September 07, 2006 02:15AM

read this

[php.net]

Options: ReplyQuote
Re: Variables magically being unset!
Posted by: fuelman (---.net82.skekraft.net)
Date: September 12, 2006 12:28PM

I have the wery same problem, ive been coding php for 6 years and used WAMP for 2 years. After reinstalling my OS ( i always keep WAMP at d:/wamp ) for the first time ive never seen this before. In functions global $varname; doesnt work .. scripts behave strange etc..

give me en real answer .. not that exit; is missing

Options: ReplyQuote
Re: Variables magically being unset!
Posted by: fuelman (---.net82.skekraft.net)
Date: September 12, 2006 08:57PM

Ive found that some mysterious things can happen when i use different saved pages, UTF-8, ISO-bla save like this and that

Options: ReplyQuote


Sorry, only registered users may post in this forum.