sql限制显示第一行失败

sql limit fail to display first row

我有一个 SQL 查询,它看起来像:

    `SELECT * FROM table LIMIT 5;`

实际上我使用 mysql 的免费员工数据库,查询是:

    `SELECT * FROM employees LIMIT 5;`

现在我有一个 PHP 函数,它将所选数据输出到 HTML table 中,如下所示:


function outputTable($query, $link){
     //Verbindung zur Datenbank inkludieren
    require_once('./config.php');
    //auffangen der Rückgabe
    $erg = mysqli_query($link, $query);
    //bestimmt Anzahl der Spalten (aocol = Amount Of COLumns)
    //counts the amount of columns
    $aocol = count(mysqli_fetch_array($erg, MYSQLI_NUM));
    //table head
    $head = "";
    for($x = 0; $x < $aocol; $x++) {
        //legt alle Informationen des Feldes $x in $finfo, darunter auch den Namen der Spalte.
        //puts all information of the field $x in $finfo, also the name of the column
        $finfo = mysqli_fetch_field_direct($erg, $x);
        //Schreibt die Kopfzeile der Tabelle
        //writes the table's head
        $head .= "<th>".$finfo->name."</th>";
    }
    //only if style.css included-->irrelevant
    echo '<div class="table"><table id="table">';
    //output of table's head
    echo "<th>$head</th>";
    //output of table's body --> here must be the bug, I think
    while($zeile = mysqli_fetch_array($erg, MYSQLI_NUM)) {
        //new tablerow
        echo "<tr>";
        //filling the row
        foreach($zeile as $feld) {
            //format for numbers
            $ra = preg_match('/^\d+$/',$feld) ? ' align="right"' : '';
            //displaying table data
            echo "<td$ra>".$feld."</td>";
        }
    }
    //closing table and layout
    echo '</table></div>';
}

我的问题是第2-5行写好了,但是第一行不见了... 如果你执行查询,你应该得到这样的东西:

| emp_no | 
|  10001 | 
|  10002 | 
|  10003 | 
|  10004 | 
|  10005 |

我实际上只得到 emp_nos 10002-10005。

您应该使用这样的查询来获取所有结果:

$query = "SELECT * FROM table ORDER BY  `id` LIMIT 0, 5";

这只会为您提供前 5 个结果,您还可以:

$query = "SELECT * FROM table ORDER BY  `id` LIMIT 5, 100";

或者这样:

$query = "SELECT * FROM table ORDER BY `id` DESC LIMIT 5";

mysqli_fetch_array 已经将结果提取为数组。

我怀疑它在获取之后移动了 mysql 游标,这就是为什么您缺少第一行的原因。

一个更简单的解决方案可能是这样的:

function outputTable($query, $link) {
    require_once('./config.php');
    $erg = mysqli_query($link, $query);
    $result = mysqli_fetch_array($erg, MYSQLI_ASSOC);
    $aocol = count($result);
    $head = "";
    foreach($result as $row) {
        $head .= '<th>' . $row['name'] . '</th>';
    }
    echo '<div class="table"><table id="table">';
    echo "<th>$head</th>";
    foreach($result as $row) {
        echo '<tr>';
        foreach($row as $field) {
            $ra = preg_match('/^\d+$/',$field) ? ' align="right"' : '';
            echo "<td$ra>".$field."</td>";
        }
    }
    echo '</table></div>';
}

当你打电话...

$aocol = count(mysqli_fetch_array($erg, MYSQLI_NUM));

这实际上是在读取第一行,因此它不再适用于以下循环。

我建议重组您的代码,以便在主读取循环内构建头部,但仅当 $head 为空时...

//table head
$head = "";

//only if style.css included-->irrelevant
echo '<div class="table"><table id="table">';
//output of table's body --> here must be the bug, I think
while($zeile = mysqli_fetch_array($erg, MYSQLI_NUM)) {
    if ( empty ($head) )    {
        //counts the amount of columns
        $aocol = count($zeile);
        for($x = 0; $x < $aocol; $x++) {
            //legt alle Informationen des Feldes $x in $finfo, darunter auch den Namen der Spalte.
            //puts all information of the field $x in $finfo, also the name of the column
            $finfo = mysqli_fetch_field_direct($erg, $x);
            //Schreibt die Kopfzeile der Tabelle
            //writes the table's head
            $head .= "<th>".$finfo->name."</th>";
        }
        //output of table's head
        echo "<th>$head</th>";
    }
    //new tablerow
    echo "<tr>";
    //filling the row
    foreach($zeile as $feld) {
        //format for numbers
        $ra = preg_match('/^\d+$/',$feld) ? ' align="right"' : '';
        //displaying table data
        echo "<td$ra>".$feld."</td>";
    }
}

此外,如果您将 MYSQLI_NUM 更改为 MYSQLI_ASSOC,那么您可以只使用键名作为列名并删除额外的 API 调用...

while($zeile = mysqli_fetch_array($erg, MYSQLI_ASSOC)) {
    if ( empty ($head) )    {
        foreach ( $zeile as $name => $value )   {
            $head .= "<th>".$name."</th>";
        }
        //output of table's head
        echo "<th>$head</th>";
    }