mysqli_fetch_assoc 不起作用[重复]

问题描述 投票:0回答:2

为什么第二次尝试时没有显示第一行?在屏幕截图中,第一次回显时第一行突出显示。

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";
php mysqli
2个回答
1
投票
$row = mysqli_fetch_assoc($result3); 

因为当您在循环之前执行此操作时,您会将指针移动到下一行,因此循环从第二行开始。

在循环之前,您可以将指针重置到第一行

mysqli_data_seek($result3,0);

0
投票

因为,您执行了 mysqli_fetch_assoc 两次,而没有休息指针。 Hanky Panky 说得对。

© www.soinside.com 2019 - 2024. All rights reserved.