输出中缺少行

时间:2012-11-21 10:16:08

标签: php mysql select

我有一个小问题,我找不到答案。

我有一个名为product_features的表,其中包含以下字段:

product_features
----------------------
 id  parent_id  name      order    added_date

现在的问题是我刚刚插入了两行具有相同名称的行:

734     1   6008-2 test     0   2012-11-21 11:52:28
735     1   6008-2 test     0   2012-11-21 11:57:27

我已定义id - PRIMARY INDEX - AUTO INCREMENTparent_id - INDEX

Keyname Type    Unique  Packed  Column     Cardinality  Collation
PRIMARY BTREE   Yes      No product_feature_id  671         A       
product_feature_parent_id   BTREE   No  No  product_feature_parent_id   671 A

当我打印结果时,打印的唯一行是最后一行..我真的不明白为什么。这是我正在使用的功能:

public function printProductFeaturesTable($product_feature_parent_id = 0, $level = 0){
        $sql = $this->mysqli->query("SELECT product_feature_id, product_feature_name
                                     FROM product_features
                                     WHERE product_feature_parent_id='" . $product_feature_parent_id . "'
                                     ORDER BY product_feature_order");
        if($sql->num_rows != 0) {

            if($level != 0)
                echo '<table cellpadding="0" cellspacing="1" class="dnd"><tbody>';

            $i = 0;
            while($row = $sql->fetch_assoc()) {
                $i++;
                echo '<tr>
                        <td width="30" valign="top" align="center">', 
                            $i, '.
                        </td>
                        <td>', 
                            $row['product_feature_name'],
                            '<div class="table-options">
                                <input type="hidden" name="product_feature_id[]" value="', $row['product_feature_id'], '" /> 
                                <a href="/admin/product-features/add-modify/', $row['product_feature_id'], '" class="ui-state-default ui-corner-all left"><span class="ui-icon ui-icon-pencil"></span></a>
                                <a href="javascript:if(confirm(\'Are you sure you want to delete this feature?\')) document.location = \'/admin/resources/php/product-features.php?p=delete&pfid=', $row['product_feature_id'], '\'" class="ui-state-default ui-corner-all left"><span class="ui-icon ui-icon-close"></span></a>
                            </div>';

                $this->printProductFeaturesTable($row['product_feature_id'], $level + 1);

                echo '  </td>
                    </tr>'; 
            }

            if($level != 0)
                echo '</tbody></table>';
        } else
            if($product_feature_parent_id == 0)
                echo '<tr><td align="center" colspan="2">Nici o specificatie de produs adaugata momentan!</td></tr>';
    }

结果应该是这样的:

1. Test
   1. Test subcategory
   2. Test subcategory 2
   3. 6008-2 test
   4. 6008-2 test

但不是上面的例子,结果是:

1. Test
   1. Test subcategory
   2. Test subcategory 2
   3. 6008-2 test // this is the last inserted row.. but where is the other one???

先谢谢你了!

1 个答案:

答案 0 :(得分:1)

好吧..我刚刚发现唯一的问题是我的眼睛,因为你可以看到我按product_feature_order排序结果,在本例中默认为0。

所以问题是它们显示的顺序不是插入的id的顺序,这真的让我很困惑。

感谢您的时间!