Re: Extracting result - unknown number of fields

From: Date: Thu, 14 Aug 2003 16:12:01 +0000
Subject: Re: Extracting result - unknown number of fields
References: 1  Groups: php.db 
Request: Send a blank email to php-db+get-29892@lists.php.net to get a copy of this message
Dillon, John wrote:
OK, please ignore, I believe I've found the answer in the manual notes: $query="your SQL"; $result=mysql_query($query) or die("Query ($query) sucks!"); $fields=mysql_num_fields($result); echo "<table>\n<tr>"; for ($i=0; $i < mysql_num_fields($result); $i++) //Table Header
Calculate the value for mysql_num_fields() outside of your for() loop to improve effeciency. $num_fields = mysql_num_fields($result); for($i=0; $i < $num_fields; $i++) // Table Header
{ print "<th>".mysql_field_name($result, $i)."</th>"; } echo "</tr>\n"; while ($row = mysql_fetch_row($result)) { //Table body echo "<tr>"; for ($f=0; $f < $fields; $f++) { echo "<td>$row[$f]</td>"; } echo "</tr>\n";} echo "</table> http://www.php.net/manual/en/function.mysql-num-fields.php
-- ---John Holmes... Amazon Wishlist: www.amazon.com/o/registry/3BEXC84AB3A5E/ PHP|Architect: A magazine for PHP Professionals – www.phparch.com

« previous php.db (#29892) next »