On Wed, Jan 07, 2004 at 09:17:35AM +1100, Martin Towell wrote: > This is probably more like what you need. > I can't see why you'd need to loop through your results using two different > methods (while and for) > > require 'database.php'; > $t_02 = "subnets"; > $sql_subs = mysql_query("SELECT * FROM $t_02",$db) or die(mysql_error()); > $num = mysql_num_rows($sql_subs); > for ($z = 0; $z < $num; $z++) > { > list($id, $sub, $msk, $dns01, $dns02, $rtrs, $rnge) = > mysql_fetch_array($sql_subs); > $vlans[] = "subnet $sub<br>netmask $msk {<br>option domain-name-servers > $dns01, $dns02;<br>option routers $rtrs;<br>range $rnge;<br>}<br>"; > } > // Write everything out formated... > for ($z = 0; $z < $num; $z++) > echo "$vlans[$z]<br />\n";
No need to calculate the number of rows: <?php require 'database.php'; $t_02 = "subnets"; $sql_subs = mysql_query("SELECT * FROM $t_02",$db) or die(mysql_error()); while (list($id, $sub, $msk, $dns01, $dns02, $rtrs, $rnge) = mysql_fetch_array($sql_subs)) { $tmp = "subnet $sub<br>netmask $msk {<br>option domain-name-servers $dns01, $dns02;<br>option routers $rtrs;<br>range $rnge;<br>}<br>"; print $tmp; $vlans[] = $tmp; } ?> I would recommend not simply doing a select *, but rather specifying which columns you want. That way, if your database schema changes (e.g., you add two more columns), your code won't break. joel -- [ joel boonstra | gospelcom.net ] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php