note 46363 deleted from function.mysql-fetch-assoc by aidan
| From: | aidan@php.net | Date: | Sun, 10 Oct 2004 02:04:25 +0000 |
| Subject: | note 46363 deleted from function.mysql-fetch-assoc by aidan | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-78256@lists.php.net to get a copy of this message | ||
Note Submitter: antonis_kirikos at yahoo dot gr
----
Here is a nice function for fetching rows into global variables named after the database fields,
(for example the value of
$row['Description'] is given to $Description).
You have to be cautious when you use it though, because if a variable with the same name as a field
already exists , the function will simply overwrite its value.
/**
* @return array
* @param resource $result
* @desc a new version of mysql_fetch_assoc(). Registers new global variables with field contents
and names them after the fields.
*/
function mysql_fetch_vars($result) {
if ($row=mysql_fetch_assoc($result)) {
$ok=$row;
foreach ($row as $key=>$val) {
eval("global \$$key;");
$$key=$val;
/*this is not a bug. It is similar to writing
eval("\$$key=\$val;");
*/
}
//echo "<pre>$codestr</pre><hr>";
}
else $ok=false;
return $ok;
}
/************************
* example of usage
*(a mysql connection is required)
*************************/
$sql="SELECT * FROM products";
$res=mysql_query($sql);
while (mysql_fetch_vars($res)) {
echo "<br>".(++$i)."<br>";
echo "$Id,$Description,$CategoryId,$Price<br>";
}