I’m struggling to understand why you’d want to do this, but here is how it would work using the more familiar foreach() construct.
// imagine a db query results set like this:
$results[0] ['fname'] = "Joe";
$results[0] ['lname'] = "Bloggs";
$results[1] ['fname'] = "John";
$results[1] ['lname'] = "Doe";
foreach($results as $row_outer){
foreach( $row_outer as $key => $value) {
$$key = $value;
}
// these new temporary variables are now only available INSIDE this loop
// otherwise they are going to be overwritten by the next iteration of $results
echo $fname . ' ' . $lname . PHP_EOL;
// Joe Bloggs
// John Doe
}
// outside the loop, it only contains the last rows' data
echo $fname
// John
Thanks, I’d just this minute discovered it was in fact working all along!
My need is that I want to specify certain mysql table column names and be able to work with them (UPDATES etc) rather than the all the fields. The names correpsond to an rss feed with same names.
OK, so if the list of fields is known, you’d also be better off maintaining a white-list of permitted fields too, then run a check to make sure that only your permitted fields appear in the query.
// set this somewhere where it is easy to find/edit
// should match your table column names
$permitted = array('fname','lname'); // take one of these out to test
// then add this line here
if( in_array($key, $permitted) )
$$key = $value;
Its probably worth adding that maintaining a $permitted array can also help generate your insert/update queries, seeing as you mentioned it. See this recent post here too.