<?php
/**

Prints out the Results of $query on database connection $link
using HTML table delimiters for formatting

Note $query is just a SQL query, store in PHP String format,
For example
$query = "select * from inventory";  
Will Display contents of table inventory,
Note PHPMyAdmin can generate the PHP Query String.

@param $link Active Database Connection username, password and selected DB
@param $query SQL Query to send using $link
@param $comment mySQL comment for the sent query

*/

function printQueryTable( $link, $query, $comment )
{
    echo "<table border=\"1\" width=\"50%\" align=\"center\">";
    echo "<caption>#$comment<br />";
    echo "Sending query $query</caption>";
        
    /** Send the query to link save output results */
    $result = mysqli_query( $link, $query );
    
        echo "<tr>";
    /** fetch fields names as table header */
        $fields = mysqli_fetch_fields( $result );
        for( $i = 0; $i < count( $fields ) ; ++$i )
        echo "<th>". $fields[ $i ]->name ."</th>";
    echo "</tr>";
    
    /* enumerate each row of query result
           output with td,tr delimiters */
    while( $row = mysqli_fetch_row( $result ) )
    {
    echo "<tr>";
    for( $i = 0; $i < count( $row ) ; ++$i )
        echo "<td>".$row[$i]."</td>";

     echo "</tr>";
    }
    
    echo "</table><br />";
        
    mysqli_free_result( $result );
}
?>