【问题标题】:SQL Query Results not printing the first row itemsSQL 查询结果不打印第一行项目
【发布时间】:2012-08-15 19:50:43
【问题描述】:

我想将查询结果打印到表格中。我在查询中设置了一个限制,只能获取 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++;
}

?>

【问题讨论】:

  • 取消限制后会发生什么?那么所有行都显示了吗?另外,显示使用的确切 SQL 查询

标签: php mysql html-table row


【解决方案1】:

没有任何 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++;
}

?>

【讨论】:

    【解决方案2】:

    谢谢你们的回答!

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

    $row = mysql_fetch_assoc($result);
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-09-28
      • 1970-01-01
      • 2022-12-10
      • 2011-07-26
      • 1970-01-01
      • 1970-01-01
      • 2010-12-28
      • 1970-01-01
      相关资源
      最近更新 更多