jonnewton Posted December 21, 2007 Share Posted December 21, 2007 I want to show all the data in a MYSQL table in a series of rows in an HTML table, and can do so with the code listed below. What I would like to do is add an additional row at the top that contains headers for the HTML table using field names retrieved somehow from the MYSQL table itself. I don't know enough about MYSQL code to know how to do this. I know I could hard code the field names, but I want to use this code with different tables which might have different field configuration. Any guidance would be appreciated! Here's the code that shows just the data in a series of rows: $resource=mysql_connect($this->hostname, $this->username, $this->password); $my_db = mysql_select_db($this->databasename, $resource); $query = "SELECT * FROM $this->table WHERE term = \"$this->term\" AND year = \"$this->year\" ORDER BY last"; $result = mysql_query ($query); ?> <table border="0" cellspacing="1" cellpadding="5"> <?php while($row = mysql_fetch_array($result, MYSQL_ASSOC)) { echo "<tr class=\"$row_color\">"; foreach($row as $data) { $this->draw_cell($data); } echo "</tr>"; } echo "</table>"; Quote Link to comment https://forums.phpfreaks.com/topic/82704-solved-using-mysql-table-field-names-as-headers-in-a-html-table/ Share on other sites More sharing options...
PHP_PhREEEk Posted December 21, 2007 Share Posted December 21, 2007 Try using mysql_field_name(). Once that page is loaded, the left sidebar contains all the other possible mysql functions built into PHP. Dig right on in... = ) PhREEEk Quote Link to comment https://forums.phpfreaks.com/topic/82704-solved-using-mysql-table-field-names-as-headers-in-a-html-table/#findComment-420663 Share on other sites More sharing options...
jonnewton Posted December 21, 2007 Author Share Posted December 21, 2007 OK, very cool. I will start sorting that out! Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/82704-solved-using-mysql-table-field-names-as-headers-in-a-html-table/#findComment-420666 Share on other sites More sharing options...
jonnewton Posted December 21, 2007 Author Share Posted December 21, 2007 OK, that works great. Here's my code: $fields = mysql_num_fields($result); echo "<tr class=\"rowColor1\">"; for ($i = 0; $i < $fields; ++$i) { $this->draw_cell(mysql_field_name($result, $i)); } echo "</tr>"; Thanks again for your help! Quote Link to comment https://forums.phpfreaks.com/topic/82704-solved-using-mysql-table-field-names-as-headers-in-a-html-table/#findComment-420676 Share on other sites More sharing options...
PHP_PhREEEk Posted December 21, 2007 Share Posted December 21, 2007 No prob Jon - Welcome to PHP Freaks! PhREEEk Quote Link to comment https://forums.phpfreaks.com/topic/82704-solved-using-mysql-table-field-names-as-headers-in-a-html-table/#findComment-420678 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.