SDJ SDJ - 1 year ago 54
SQL Question

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) : ?>

print '<table class="col-md-12">
<th class="col-md-1">Category</th>
<th class="col-md-2">Subcategory</th>
//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>'";


Answer Source

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.