SQL查询结果不打印第一行项

时间:2012-08-20 13:14:56

标签: php mysql html-table row

我想将查询结果打印到表格中。我在查询中设置了一个限制,仅获取5行。它确实从我的数据库中获取了5行。但是当我将结果打印到HTML表格中时,结果中的第一行不会打印出来。我发现我的代码没有任何问题。求救!

我是这样做的:

<table style="border:none; width:790px;; margin:10px;" cellpadding="0" cellspacing="0">
    <tr>
        <th  class="header">ID</th>
        <th  class="header">Client Name</th>
        <th  class="header">Driver's License</th>
        <th  class="header">Social Security</th>
        <th  class="header">Phone Number</th>
        <th  class="header">Email Address</th>
        <th  class="header">Date Signed Up</th>
    </tr>  
<?php

$i=1;
while($row = mysql_fetch_assoc($result)){
    if ($i & 1) { //odd rows
        echo '<tr>';
        echo '<td class="entry_odd">'.$row['cid'].'</td>';
        echo '<td class="entry_odd">'.$row['fname'].' '.$row['lname'].'</td>';
        echo '<td class="entry_odd">'.$row['license'].'</td>';
        echo '<td class="entry_odd">'.$row['ss1'].' / '.$row['ss2'].' / '.$row['ss3'].'</td>';
        echo '<td class="entry_odd">'.$row['phone1_1'].'-'.$row['phone1_2'].'-'.$row['phone1_3'];
            if ($row['phone1_ext'] != NULL ) {
                echo ' ext. '.$row['phone1_ext'].'</td>';
            } else { 
                echo '</td>';
            }
        echo '<td class="entry_odd">'.$row['email'].'</td>';
        echo '<td class="entry_odd">'.$row['registered'].'</td>';
        echo '</tr>';

    } else { //even rows
        echo '<tr>';
        echo '<td class="entry_even">'.$row['client_id'].'</td>';
        echo '<td class="entry_even">'.$row['fname'].' '.$row['lname'].'</td>';
        echo '<td class="entry_even">'.$row['license'].'</td>';
        echo '<td class="entry_even">'.$row['ss1'].' / '.$row['ss2'].' / '.$row['ss3'].'</td>';
        echo '<td class="entry_even">'.$row['phone1_1'].'-'.$row['phone1_2'].'-'.$row['phone1_3'];
            if ($row['phone1_ext'] != NULL ) {
                echo ' ext. '.$row['phone1_ext'].'</td>';
            } else { 
                echo '</td>';
            }
        echo '<td class="entry_even">'.$row['email'].'</td>';
        echo '<td class="entry_even">'.$row['registered'].'</td>';
        echo '</tr>';
    }

        $i++;
}

?>

2 个答案:

答案 0 :(得分:0)

如果没有任何SQL,我会做出以下更改:

<?php

$i = 0;

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

    $row_class = ($i % 2) ? "entry_even" : "entry_odd";

    echo '<tr>';
    echo '<td class="' . $row_class . '">'.$row['cid']."</td>\n";
    echo '<td class="' . $row_class . '">'.$row['fname'].' '.$row['lname']."</td>\n";
    echo '<td class="' . $row_class . '">'.$row['license']."</td>\n";
    echo '<td class="' . $row_class . '">'.$row['ss1'].' / '.$row['ss2'].' / '.$row['ss3']."</td>\n";
    echo '<td class="' . $row_class . '">'.$row['phone1_1'].'-'.$row['phone1_2'].'-'.$row['phone1_3'];
        if ($row['phone1_ext'] != NULL ) {
        echo ' ext. '.$row['phone1_ext']."</td>\n";
        } else { 
        echo "</td>\n";
        }
    echo '<td class="' . $row_class . '">'.$row['email']."</td>\n";
    echo '<td class="' . $row_class . '">'.$row['registered']."</td>\n";
    echo '</tr>';

    $i++;
}

?>

答案 1 :(得分:0)

感谢您的回答!

但我发现搞砸了这件事。这是我在查询之后和$i=1;之前放置的一行。这就是那条线:

$row = mysql_fetch_assoc($result);

这已经从数组中获取了第一行,这就是为什么当我通过循环提供数组时,第一行已经被删除了。