2016-04-15 5 views
0

Warum die erste Zeile wird nicht angezeigt, wenn ich es beim zweiten Mal versuche? Im Screenshot wird die erste Zeile beim ersten Echo hervorgehoben.mysqli_fetch_assoc funktioniert nicht

output

[![enter image description here][1]][1]/*================================================================================================================= 
     mysqli_fetch_assoc - returns data in an associative array 
     mysqli_fetch_row - returns data in a numeric array 
     mysqli_fetch_array - returns data in both an associative and a numeric array 
    */ 
    $query2 = "SELECT * FROM User"; 
    //$result3 = mysqli_query ($con, $query2) or die ("Couldn't execute SELECT query"); 
    $result3 = mysqli_query ($con, $query2) or die (mysqli_error($con)); 

    $row = mysqli_fetch_assoc($result3); //returns an array called $row with column names as keys. Get one row of data 
             //If you need more than one row of data, use the function in a loop 

    echo $row['Title'] ." - ". $row['FName'] ." - ". $row['LName']; //one row of data. It is fine when checking a password 

    //extract function - splits the array into variables that have same name as the key 
    extract($row); 
    echo "<hr>".$FName; //variable having same name as the key, also identical to column name 
    echo "<br>Number of rows in the User table: ".mysqli_num_rows($result3); 

    echo "<table>"; 
    echo "<th>User Id</th> <th>Title</th> <th>First Name</th> <th>Last Name</th> <th>Email</th>"; 
    while($row = mysqli_fetch_assoc($result3)){ 
     extract($row); 
     echo "<tr>"; 
     echo "<td>".$UserId."</td>" . "<td>".$Title."</td>" . "<td>".$FName."</td>" . "<td>".$LName."</td>" . "<td>".$Email."</td>" ; 
     echo "</tr>"; 
    } 
    echo "</table>"; 

    echo "<hr>My connection to the MySQL database"; 
+0

Sie haben die erste Zeile bereits mit '$ row = mysqli_fetch_assoc ($ result3);' mehrere Zeilen vor Ihrer 'while' Schleife abgerufen. – mitkosoft

Antwort

1
$row = mysqli_fetch_assoc($result3); 

Denn wenn Sie das tun, vor der Schleife, können Sie den Zeiger auf nächste Zeile zu bewegen, so dass Ihre Schleife aus der zweiten Reihe beginnt.

Vor der Schleife können Sie den Zeiger auf der 1. Zeile zurückgesetzt

mysqli_data_seek($result3,0); 
+0

vielen Dank. danke – Praba

0

Weil Sie mysqli_fetch_assoc zweimal tun, ohne den Zeiger restting. Hanky ​​Panky sagte es richtig.

+0

Was bedeutet "Zeiger zurücksetzen"? Ich bin Neuling. Vielen Dank, wenn Sie dem Code-Fragment eine kurze Beschreibung geben können. – Praba

+0

vielen Dank Bruder. Ich habe verstanden – Praba

Verwandte Themen