ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button
Article:
  Tips for Building Web Database Applications with PHP and MySQL
Subject:   Problem using foreach to access mysql_fetch_array
Date:   2002-11-28 09:59:33
From:   ddandrea
The article mentions that you can use the foreach construct to make your code for dumping database query results more readable. Thus:


foreach ($row as $field) echo "<td>$field</td>";


However, if you don't set the result_type parameter of mysql_fetch_array(), this doesn't work properly: each field is accessed and printed twice. I think this is because it goes through each $row element, as numerically indexed, and also as associatively indexed.


To get this to work, set the result_type parameter, like so:
while ($row = mysql_fetch_array($query_result, MYSQL_ASSOC))


Otherwise, the article and the associated book are very good, highly recommended! (at least for newbies like me)