猿问

sql限制无法显示第一行

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

    `SELECT * FROM table LIMIT 5;`

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

    `SELECT * FROM employees LIMIT 5;`

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

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>';

}


翻过高山走不出你
浏览 116回答 2
2回答

慕婉清6462132

你打电话时...$aocol = count(mysqli_fetch_array($erg, MYSQLI_NUM));这实际上是在读取第一行,因此它不再可用于以下循环。我建议重组您的代码,以便在主读取循环内构建头部,但只有当$head它为空时......//table head$head = "";//only if style.css included-->irrelevantecho '<div class="table"><table id="table">';//output of table's body --> here must be the bug, I thinkwhile($zeile = mysqli_fetch_array($erg, MYSQLI_NUM)) {&nbsp; &nbsp; if ( empty ($head) )&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; //counts the amount of columns&nbsp; &nbsp; &nbsp; &nbsp; $aocol = count($zeile);&nbsp; &nbsp; &nbsp; &nbsp; for($x = 0; $x < $aocol; $x++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //legt alle Informationen des Feldes $x in $finfo, darunter auch den Namen der Spalte.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //puts all information of the field $x in $finfo, also the name of the column&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $finfo = mysqli_fetch_field_direct($erg, $x);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Schreibt die Kopfzeile der Tabelle&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //writes the table's head&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $head .= "<th>".$finfo->name."</th>";&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; //output of table's head&nbsp; &nbsp; &nbsp; &nbsp; echo "<th>$head</th>";&nbsp; &nbsp; }&nbsp; &nbsp; //new tablerow&nbsp; &nbsp; echo "<tr>";&nbsp; &nbsp; //filling the row&nbsp; &nbsp; foreach($zeile as $feld) {&nbsp; &nbsp; &nbsp; &nbsp; //format for numbers&nbsp; &nbsp; &nbsp; &nbsp; $ra = preg_match('/^\d+$/',$feld) ? ' align="right"' : '';&nbsp; &nbsp; &nbsp; &nbsp; //displaying table data&nbsp; &nbsp; &nbsp; &nbsp; echo "<td$ra>".$feld."</td>";&nbsp; &nbsp; }}此外,如果您更改MYSQLI_NUM为MYSQLI_ASSOC,那么您可以只使用键名作为列名并删除额外的 API 调用......while($zeile = mysqli_fetch_array($erg, MYSQLI_ASSOC)) {&nbsp; &nbsp; if ( empty ($head) )&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; foreach ( $zeile as $name => $value )&nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $head .= "<th>".$name."</th>";&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; //output of table's head&nbsp; &nbsp; &nbsp; &nbsp; echo "<th>$head</th>";&nbsp; &nbsp; }

至尊宝的传说

您应该使用这样的查询来获取所有结果:$query&nbsp;=&nbsp;"SELECT&nbsp;*&nbsp;FROM&nbsp;table&nbsp;ORDER&nbsp;BY&nbsp;&nbsp;`id`&nbsp;LIMIT&nbsp;0,&nbsp;5";这只会给你前 5 个结果,然后你可以:$query&nbsp;=&nbsp;"SELECT&nbsp;*&nbsp;FROM&nbsp;table&nbsp;ORDER&nbsp;BY&nbsp;&nbsp;`id`&nbsp;LIMIT&nbsp;5,&nbsp;100";或这个:$query&nbsp;=&nbsp;"SELECT&nbsp;*&nbsp;FROM&nbsp;table&nbsp;ORDER&nbsp;BY&nbsp;`id`&nbsp;DESC&nbsp;LIMIT&nbsp;5";
随时随地看视频慕课网APP
我要回答