It it possible to write some sort of loop to simplify lists such as the following?PHP Code:$mid= $row['mid'];
$mbid= $row['mbid'];
$make= $row['make'];
$model= $row['model'];
| SitePoint Sponsor |
It it possible to write some sort of loop to simplify lists such as the following?PHP Code:$mid= $row['mid'];
$mbid= $row['mbid'];
$make= $row['make'];
$model= $row['model'];
I'm not sure what you are trying to say.
After a mySQL query, for instance, all my data is read into $row via mysql_fetch_array(). When I use the values later in the script, I'd like to be able to refer to simply the field name rather than $row['field'], so I've written out long lists transferring the values to corresponding variables.
Is there some sort of foreach loop I can run to do this for the entire array without having to write out each variable individually?





You should be careful with this, though, in case you overwrite another variable needed by your script. You might be better off prefixing the field names, like this:PHP Code:foreach($row as $key => $value) {
$$key = $value;
}
This would prefix all the variables with r_PHP Code:foreach($row as $key => $value) {
$r_{$key} = $value;
}
Sure, take a look at extract().
Use it carefully though.
![]()
@AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.
Thanks for the function. Could you please explain why using extract() on input variables is bad?
As SJH stated, in longer scripts, it would be far too easy for extract to overwrite an existing, trusted variable into an unchecked, unsanitised user defined variable.
Havoc man, HAVOK!![]()
@AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.
Oh, didn't see SJH's post. Sorry about that.
Your foreach does the same thing as extract(), right?
Thanks![]()
Last edited by geiger; Mar 12, 2009 at 16:35.
extract has options for how it should behave should a collision or invalid variable name be encountered.





Pretty much yes, but I'd recommend the use of extract() instead of my original suggestion.
I always try and make a point of remembering that this function exists but it always slips my mind when I need to use it, and consequently end up using a foreach loop. Maybe I should get it tattooed on my forearm.
It's usefulJust to make sure, is there anything wrong with how I'm doing this?
PHP Code:$row = mysql_fetch_array( $sql);
extract( $row, EXTR_OVERWRITE );
$notes = htmlentities( $notes );
$fueltype = htmlentities( $fueltype );
$strategy= htmlentities( $strategy );
echo "$cid, $notes";
overwrite would be correct. It's a good idea to explicitly list your columns in your sql query(for more reasons than this). If you're using the star, eg SELECT *, then if you every change the table column names, or add new ones, now all of a sudden you have new variables floating around your script because of extract.
Yes, I never use *. Here's a sample of how I'm fetching data right now. I need to add parameterizing still, and also validate the data going in. Then—if I'm right—I'll be secure?PHP Code:$sql = mysql_query(
" SELECT
calc_gallons,
calc_mpg,
cid,
cpg,
enddate,
fueltype,
in_gallons,
in_miles,
in_mpg,
notes,
strategy,
tid
FROM
fe_tanks
WHERE
tid = $tid ")
or
die( mysql_error() );
$row = mysql_fetch_array( $sql );
extract( $row, EXTR_OVERWRITE );
$notes = htmlentities( $notes );
$fueltype = htmlentities( $fueltype );
$strategy = htmlentities( $strategy );
Bookmarks