Previous Page

Output database tables into an HTML table

Below is the simple code to pull info from your database and output on a webpage using an HTML table.
You can see examples of this on my Upcoming Meets and Meet Results pages on my site. Those tables pull information from a database.

NOTE: Notice when the PHP code STARTS and ENDS within the code below.

<?php  /****** START PHP ******/

/* This general query will allow you to pull any data from a table
   in your database. It will be used later in the code to pull
   specific infomation. */
    $query_result = mysql_query("SELECT * FROM `my_table` ORDER BY `id` ASC");
// The following will give you the total number of records in your table
    $number_of_rows = mysql_numrows($query_result); 
?> /****** END PHP ******/

<table> // start your table
	<thead> /*  Important to keep the table's heading outside
                   of the while code from below so it is only 
                   output once */

<?php   /****** START PHP ******/
		$id = 0;

			while ($id < $number_of_rows) /* while the table has records.
                        						As soon as the id is not less
                                                than the number of records the
                                                table will be complete       */
				$name = mysql_result($query_result,$id,"name"); /* ( the query name from above, 
                													 the id, the name of the 
                                                                     column in your table )  */
				$age = mysql_result($query_result,$id,"age");
				$birthday = mysql_result($query_result,$id,"birthday");
				$city = mysql_result($query_result,$id,"city");
				$state = mysql_result($query_result,$id,"state");
?>  /****** END PHP ******/
		<tr> /* start a row inside the while statement and output the info from 
        		   your tables columns */
			<td> <?php echo $name; ?></td> /* echo the query results using
            										  PHP inside the table columns */
			<td> <?php echo $age; ?></td>
			<td> <?php echo $birthday; ?></td>
			<td> <?php echo $city; ?></td>
			<td> <?php echo $state; ?></td>
<?php   /****** START PHP ******/

				id++; // increment the id after each record output
?>  /****** END PHP ******/
                     </table>    // END TABLE