Missing first line in query result

Hi guys,

I’ve nearly got this code working but just can’t figure why my data is missing the very first record. To explain:

i have 2 tables in my mysql database, tv_category_sub and tv_category_sub_sub. The two tables are linked together by the column tv_sub_id.

my code is as follows:

<?php
  
echo '<table cellspacing="5" cellpadding="5" align="left">';

$result = mysql_query("SELECT a.tv_sub_name, b.tv_sub_sub_name, b.tv_sub_sub_id FROM tv_category_sub_sub b LEFT JOIN tv_category_sub a ON a.tv_sub_id = b.tv_sub_id ORDER BY a.tv_sub_name, b.tv_sub_sub_name ASC") or die("SELECT error: " . mysql_error());

$columns_counter=0;

echo '<tr>';

$currentCat = '';
while($row = mysql_fetch_array($result)) {
$subcat = $row['tv_sub_name'];
$subsubid = $row['tv_sub_sub_id'];
$subsubcat = $row['tv_sub_sub_name'];

    extract($row);
    if ($currentCat != $subcat) {
        //if a new category, write category head
        echo "<tr><td colspan=\\"3\\"><h4>$subcat TV brackets</h4></td></tr>";
        $currentCat = $subcat;
        
    } else {
        echo '<td align = "left">';
echo $subsubcat  . ' - ' . $subsubid; 
    }
echo '</td>';
$columns_counter++;
if($columns_counter==3)
{
echo '</tr><tr>';
$columns_counter=0;
}
}

echo '</tr></table>';
?> 

what i what to display is this

TV-Category 1(tv_sub_name)

TV-Sub 1 TV-Sub 2 TV-Sub 3
TV-Sub 4
(tv_sub_sub_name)

TV-Category 2

TV-Sub 1 TV-Sub 2 TV-Sub 3
TV-Sub 4

…and so on

However what I’m getting is this

TV-Category 1(tv_sub_name)

TV-Sub 2 TV-Sub 3
TV-Sub 4
(tv_sub_sub_name)

TV-Category 2

TV-Sub 2 TV-Sub 3
TV-Sub 4

It looks like it’s counting the tv_sub_name (i.e the main category) as the first record and jumping to the second record for tv_sub_sub_name (the sub category). Can anyone help and show me where I’m going wrong? Hope you can understand what I’m saying.

Just shooting from the hip here. I would suggest reversing the join. Your joining b to a. I would think you’d want to join a to b.

Hi darkwater,

Thanks for the response. Tried it but it’s still the same. Will take another look today. Would really appreciate any further guidance as to where I’m going wrong.

What does var_dump( $row) tell you about the result set being returned?

Is this a PHP problem or an SQL problem?

If you establish that it is a PHP problem look carefully at the resulting HTML source code, is your table being formatted correctly?

What does var_dump( $row) tell you about the result set being returned?

Is this a PHP problem or an SQL problem?

If you establish that it is a PHP problem look carefully at the resulting HTML source code, is your table being formatted correctly?

Hi cups,

Looking at the var_dump, it must be a formatting issue. The data returned is correct and the missing first rows of data are there. I’m guessing that because the results are correct that it must be the way I’m displaying it. I’ll keep looking at the code. Thanks for the advice.

And when you look at the source code of the html page, can you see it in there at all?

Some badly nested, or un-closed <td> or <tr> elements in some browsers can be hidden by the browsers output.