Does the extract() function behave differently in PHP5?
Posted: Tue Feb 16, 2010 3:33 pm
Hi,
Quick bit of background - I learnt and used PHP quite intensively one summer about 6 years ago, but sadly I've not done much with it since, so my knowledge is rusty at best!
So anyway, purely for sentimental value, I've been trying to resurect a website I built that summer using Apache/PHP/MySQL. I'm not sure what version PHP was on back then, but I'm assuming 4 or less. I'm now hosting the rebuilt site on my own personal web server running Debian Lenny, PHP5 and MySQL Server 5.0.
It mostly works fine, but I am having a problem with one PHP function (extract) that doesn't seem to work as it used to on my original implementation, so I'm wondering if the function behaves differently in PHP5?
Example:
This query from my script returns a single row (the members details) from the site member database:
$query = mysql_query("SELECT * FROM Member WHERE loginName = '$fusername'");
while($row = mysql_fetch_array($query)){
extract($row);
}
How it (apparently) used to work was the extract function would create and populate variables named after each of the database fields from the returned row, with the value of each variable being the value in the row for that field.
For example, one of the fields in the database is joinstatus, so a variable called $joinstatus would be created, then further down my script I'd check the value and act accordingly. Examples of other variables the script uses that extract() would also create/populate include $firstname, $lastname, $joindate, $paidstatus etc...
In PHP5 however, these variables all seem to have null values after extract() has been executed. It's like the variables have never been created/initialised which is why I'm wondering if the extract function works differently...
Any insights/suggestions why this is happening, how to fix it or do it differently?
Thanks in advance.
JG
Quick bit of background - I learnt and used PHP quite intensively one summer about 6 years ago, but sadly I've not done much with it since, so my knowledge is rusty at best!
So anyway, purely for sentimental value, I've been trying to resurect a website I built that summer using Apache/PHP/MySQL. I'm not sure what version PHP was on back then, but I'm assuming 4 or less. I'm now hosting the rebuilt site on my own personal web server running Debian Lenny, PHP5 and MySQL Server 5.0.
It mostly works fine, but I am having a problem with one PHP function (extract) that doesn't seem to work as it used to on my original implementation, so I'm wondering if the function behaves differently in PHP5?
Example:
This query from my script returns a single row (the members details) from the site member database:
$query = mysql_query("SELECT * FROM Member WHERE loginName = '$fusername'");
while($row = mysql_fetch_array($query)){
extract($row);
}
How it (apparently) used to work was the extract function would create and populate variables named after each of the database fields from the returned row, with the value of each variable being the value in the row for that field.
For example, one of the fields in the database is joinstatus, so a variable called $joinstatus would be created, then further down my script I'd check the value and act accordingly. Examples of other variables the script uses that extract() would also create/populate include $firstname, $lastname, $joindate, $paidstatus etc...
In PHP5 however, these variables all seem to have null values after extract() has been executed. It's like the variables have never been created/initialised which is why I'm wondering if the extract function works differently...
Any insights/suggestions why this is happening, how to fix it or do it differently?
Thanks in advance.
JG