PHP MYSQL关联数组和表

Here is a code. This loads all the header part (i.e the header for the table) dynamically from the database.

The below code works fine. But the column is mismatched. i.e. the first row first column of the header is blank and there is a dislocation in the table.

Code

    <table border="1">
    <?php
    $book_query = mysql_query("select * from book_master");
    $i = 0;
    while($row = mysql_fetch_assoc($book_query))
    {

            $columns = array_keys($row);
            ?> 
            <th>
                <?php
                foreach($columns as $column)
                {
                ?>
                    <td><?php echo $column; ?> </td>

            </th>
            <?php 
        } 
        ?>
        <tr>
            <?php
            foreach($row as $key=>$value)
            {
                ?>
                <td><?php echo $value; ?></td>
                <?php
            }
            ?>
        </tr>
        <?php
        $i++;
    }
    ?>          
    </table>

EDIT:

Here is my print_r($columns) value:

Array ( [0] => Author Name [1] => Book Name [2] => Rating [3] => Location )

I know the problem is with the loop. Could someone help me out?

Hope this will help someone. Just I have replaced the TH tag with TR and the output is perfect.

    <table border="1">
    <?php
    $book_query = mysql_query("select * from book_master");

    while($row = mysql_fetch_assoc($book_query))
    {

    $columns = array_keys($row);
    ?> 
    <tr>
    <?php
    foreach($columns as $column){ ?>
    <td><?php echo $column; ?> </td>
    <?php } ?>
    </tr>

    <tr>
    <?php
    foreach($row as $key=>$value){ ?>
    <td><?php echo $value; ?></td>
    <?php } ?>
    </tr>


    </table>

Just remove TH tag because its create one extra cell, so your layout messed up

<table border="1">
    <?php

    $book_query = mysql_query("select * from book_master");
    $i = 0;
    while($row = mysql_fetch_assoc($book_query))
    {
    if($i == 0){
    $columns = array_keys($row);
    ?> 
    <?php
    foreach($columns as $column){ ?>
    <td><?php echo $column; ?> </td>
    <?php } ?>
    <?php } ?>
    <tr>
    <?php
    foreach($row as $key=>$value){ ?>
    <td><?php echo $value; ?></td>
    <?php } ?>
    </tr>
    <?php
    $i++;
    }
    ?>          
    </table>

You can try to change

         <th>
            <?php
            foreach($columns as $column)
            { ?>
                <td><?php echo $column; ?> </td>
            <?php 
            }   
            ?>
        </th>

to

        <tr>
            <?php
            foreach($columns as $column)
            { ?>
                <th><?php echo $column; ?> </th>
            <?php 
            }   
            ?>
        </tr>