note 46363 added to function.mysql-fetch-assoc

From: Date: Fri, 08 Oct 2004 08:52:31 +0000
Subject: note 46363 added to function.mysql-fetch-assoc
Groups: php.notes 
Request: Send a blank email to php-notes+get-78175@lists.php.net to get a copy of this message
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>"; } ---- Manual Page -- http://www.php.net/manual/en/function.mysql-fetch-assoc.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+46363 Delete -- http://master.php.net/manage/user-notes.php?action=delete+46363&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+46363&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#78175) next »