note 12712 deleted from function.mysql-fetch-array by betz

From: Date: Wed, 05 Oct 2005 11:33:20 +0000
Subject: note 12712 deleted from function.mysql-fetch-array by betz
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-96428@lists.php.net to get a copy of this message
Note Submitter: chuck at eric dot uoregon dot edu ---- An easier way to accomplish what Jean.Francois did with his code is to use the mysql_fetch_assoc() function to retrieve the associative array and feed this into a while loop: <? . . . $q_result = mysql_query($select_stmt); while ($ary = mysql_fetch_assoc($q_result)) { while (list($key,$val) = each($ary)) { $$key = $val; } # You can now use your column names # as variables. } . . . ?> The above approach is suggested by Rasmus Lerdorf in the PHP Pocket Reference. It eliminates a query to the database to retrieve column names (SHOW COLUMNS) and makes prettier code, IMO. I might add that the (mis)behavior of mysql_fetch_array() in not producing column name hash elements had my stymied for a while. I find it more intuitive to use column names rather than indices to access field data. Frankly, I don't see the rationale for not producing column name hash elements when the value is NULL. Luckily, mysql_fetch_assoc() exists, making my life easier and my code more readily understood.

« previous php.notes (#96428) next »