2016-04-15 42 views
0

爲什麼我第二次嘗試時第一行沒有顯示?在屏幕截圖中,第一行在第一次回顯時突出顯示。mysqli_fetch_assoc不能正常工作

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

您已經用'$ row = mysqli_fetch_assoc($ result3);'''while''循環之前的幾行獲取第一行。 – mitkosoft

回答

1
$row = mysqli_fetch_assoc($result3); 

因爲當你這樣做的循環之前,您將指針移動到下一行,以便你的循環,從第二排開始。

您的循環之前,你可以因爲,你在做mysqli_fetch_assoc兩次,沒有restting指針,指針復位到第一排

mysqli_data_seek($result3,0); 
+0

非常感謝。謝謝 – Praba

0

。 Hanky Panky說得對。

+0

「重置指針」是什麼意思?我是新手。非常感謝,如果您可以給出簡要說明的代碼片段。 – Praba

+0

非常感謝兄弟。我明白了 – Praba