cyprus Posted September 24, 2006 Share Posted September 24, 2006 Can someone spot why this is not working properley. Its supposed to dynamically create an HTML table from a database query. But the columns are getting repeated, and the captions are misplaced. Many thanks[code]<?phpif (($result)||(mysql_errno == 0)) { echo "<table border='1' bordercolor='#000000' bgcolor='#FFFFCC' width='80%'><tr>"; if (mysql_num_rows($result)>0) { //loop thru the field names to print the correct headers $i = 0; while ($i < mysql_num_fields($result)) { echo "<td align='left'>". mysql_field_name($result, $i) . "</td>"; //$i++; } echo "</tr>"; //display the data while ($rows = mysql_fetch_array($result)) { echo "<tr>"; foreach ($rows as $data) { echo "<td align='center'>". $data . "</td>"; } } }else{ echo "<tr><td colspan='" . ($i+1) . "'>No Results found!</td></tr>"; } echo "</table>"; }else{ echo "Error in running query :". mysql_error(); } ?> [/code]MOD EDIT code tags added Link to comment https://forums.phpfreaks.com/topic/21852-dynamic-html-table-from-mysql-query-problem-solved/ Share on other sites More sharing options...
Barand Posted September 24, 2006 Share Posted September 24, 2006 Use mysql_fetch_assoc() instead of mysql_fetch_array()By default, fetch array gets the values twice, numeric index and fieldname index Link to comment https://forums.phpfreaks.com/topic/21852-dynamic-html-table-from-mysql-query-problem-solved/#findComment-97588 Share on other sites More sharing options...
cyprus Posted September 24, 2006 Author Share Posted September 24, 2006 Many thanks Barand, mysql_fetch_assoc($result) fixed my problem. Best regards Link to comment https://forums.phpfreaks.com/topic/21852-dynamic-html-table-from-mysql-query-problem-solved/#findComment-97591 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.