sql limit fail to display first row

John Doe :

I have a SQL-query which looks like:

    `SELECT * FROM table LIMIT 5;`

in fact I use mysql's free employees database and the query is:

    `SELECT * FROM employees LIMIT 5;`

now I have a PHP function which outputs the selected data into a HTML table which looks like this:


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

My problem is that rows 2-5 are written, but the first row is missing... if you execute the query, you should get something like this:

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

I in fact get only the emp_nos 10002-10005.

Nigel Ren :

When you call...

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

this is in fact reading the first line and therefore it is no longer available for the following loop.

I would suggest restructuring your code so that the head is built up inside the main read loop, but only when $head is empty...

//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>";
    }
}

Also, if you changed MYSQLI_NUM to MYSQLI_ASSOC, then you can just use the key names as the column name and remove the extra API calls...

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>";
    }

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=402145&siteId=1