简体   繁体   中英

How to pull data from MySQL with PHP into rows?

I am not sure what I am missing. The page loads with the table but does not pull any data into the rows. I connected to the database and have pulled data before I edited the code to this. And the first line does show the 9 rows I have.

<?php if (mysql_num_rows($result) > 0) : ?>
        <?php

        print '<table class="col-md-12">
            <tr>
                <th class="col-md-1">Category</th>
                <th class="col-md-2">Subcategory</th>
            </tr>
 <tr>'.$rowsfortable.'
</tr>
</table>';
//Get Category.


while ($row = mysql_fetch_array($result)) {
            $qry = "SELECT * FROM general_category WHERE categoryID = {$row['categoryID']}";
            $tmpResult = qry($qry);
            $tmpRow = mysql_fetch_array($tmpResult);
            $category = $tmpRow['category_desc'];

            //Get Sub Category.
            $qry = "SELECT * FROM general_category WHERE categoryID = {$row['sub_categoryID']}";
            $tmpResult = qry($qry);
            if (mysql_num_rows($tmpResult) > 0) {
                $tmpRow = mysql_fetch_array($tmpResult);
                $sub_category = "/ " . $tmpRow['category_desc'];
                } else {
                    $sub_category = '';
                }
 for ($i=0; $i < count($row); $i++) {
            $rowsfortable = "print '<td>' '.$category.' '.$sub_category.'</td>'";


    }
}   
?>

It looks to me like you're printing out your table before $rowsfortable is ever assigned any value. PHP will execute in order.

Try moving the block of code that grabs your data and assigns it to $rowsfortable before the block that actually prints the table.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM