当php使用limit选择时出错,从sql偏移

sg3maiej  于 2021-06-15  发布在  Mysql
关注(0)|答案(2)|浏览(377)

我正在尝试从数据库中选择数据。php脚本是从数据库表的第一行开始选择4行,但是代码最终只从第二行开始选择3行,而不是从第一行开始。我该怎么办?

<?php 

        try{

            require 'mysqli_connect.php';

            $maxrow = 4;
            $start = 0;

            $query = "SELECT user_id , first_name , last_name, email , registration_date FROM users LIMIT ? , ?";
            $stmt = mysqli_stmt_init($dbcon);
            mysqli_stmt_prepare($stmt , $query);
            mysqli_stmt_bind_param($stmt , 'ii' , $start ,$maxrow); // still showing only three rows and does not count from zero
            mysqli_stmt_execute($stmt);
            $record = mysqli_stmt_get_result($stmt);
            $row = mysqli_fetch_array($record , MYSQLI_NUM);

            if($record){

                echo "<table>";
                echo "<tr>";
                echo "<th>user_id</th>";
                echo "<th>first_name</th>";
                echo "<th>last_name</th>";  
                echo "<th>email</th>";
                echo "<th>registration_date</th>";
                echo "</tr>";

                while ($row = mysqli_fetch_array($record , MYSQLI_NUM)) {

                    $user_id = $row[0];
                    $first_name = $row[1]; 
                    $last_name = $row[2];
                    $email = $row[3];
                    $registration_date  = $row[4];

                    echo '<tr>
                    <td><a href="edit_user.php?id=' . $user_id . '">Edit</a></td>
                    <td><a href="delete_user.php?id=' . $user_id . '">Delete</a></td>
                    <td>' . $last_name . '</td>
                    <td>' . $first_name . '</td>
                    <td>' . $email . '</td>
                    <td>' . $registration_date . '</td>
                    </tr>';
                }
                echo "</table>";
                mysqli_free_result ($record); // Free up the resources. 

            }

        }catch(Exception $e){
            //echo $e->getMessage();
        }catch(Error $e){
            //echo $e->getMessage();
        }

    ?>
w3nuxt5m

w3nuxt5m1#

此处选择第一行 $row = mysqli_fetch_array($record , MYSQLI_NUM); . 所以 mysqli_fetch_array()while 从第二行开始。

c2e8gylq

c2e8gylq2#

您的第一个记录是fetch here: $row = mysqli_fetch_array($record , MYSQLI_NUM); 应该是的

<?php 

        try{

            require 'mysqli_connect.php';

            $maxrow = 4;
            $start = 0;

            $query = "SELECT user_id , first_name , last_name, email , registration_date FROM users LIMIT ? , ?";
            $stmt = mysqli_stmt_init($dbcon);
            mysqli_stmt_prepare($stmt , $query);
            mysqli_stmt_bind_param($stmt , 'ii' , $start ,$maxrow); // still showing only three rows and does not count from zero
            mysqli_stmt_execute($stmt);
            $record = mysqli_stmt_get_result($stmt);
            // Your first record is fetch here! OMG!!
            // $row = mysqli_fetch_array($record , MYSQLI_NUM);

            if($record){

                echo "<table>";
                echo "<tr>";
                echo "<th>user_id</th>";
                echo "<th>first_name</th>";
                echo "<th>last_name</th>";  
                echo "<th>email</th>";
                echo "<th>registration_date</th>";
                echo "</tr>";

                while ($row = mysqli_fetch_array($record , MYSQLI_NUM)) {

                    $user_id = $row[0];
                    $first_name = $row[1]; 
                    $last_name = $row[2];
                    $email = $row[3];
                    $registration_date  = $row[4];

                    echo '<tr>
                    <td><a href="edit_user.php?id=' . $user_id . '">Edit</a></td>
                    <td><a href="delete_user.php?id=' . $user_id . '">Delete</a></td>
                    <td>' . $last_name . '</td>
                    <td>' . $first_name . '</td>
                    <td>' . $email . '</td>
                    <td>' . $registration_date . '</td>
                    </tr>';
                }
                echo "</table>";
                mysqli_free_result ($record); // Free up the resources. 

            }

        }catch(Exception $e){
            //echo $e->getMessage();
        }catch(Error $e){
            //echo $e->getMessage();
        }

    ?>

相关问题