Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php中的分页问题_Php_Mysql - Fatal编程技术网

php中的分页问题

php中的分页问题,php,mysql,Php,Mysql,当我点击“年度班级”时,一名学生毕业了,如下图所示 它将用户带到students.php,其中包含当年毕业的学生名单 然后我使用这段代码检查members.php url字符串并从中获取值。 在students.php页面中,我实现了一个分页,它也重定向到同一个studenter code hereents.php页面 问题是,当我单击students.php页面上显示的页码时,在同一students.php页面中没有返回任何数据。 我只是想知道问题是否是这段代码的结果: 当它再次检查u

当我点击“年度班级”时,一名学生毕业了,如下图所示

它将用户带到students.php,其中包含当年毕业的学生名单

然后我使用这段代码检查members.php url字符串并从中获取值。

在students.php页面中,我实现了一个分页,它也重定向到同一个stud
enter code here
ents.php页面

问题是,当我单击students.php页面上显示的页码时,在同一students.php页面中没有返回任何数据。

我只是想知道问题是否是这段代码的结果: 当它再次检查url中不再存在的classof时

我在students.php上的代码如下:

$results_per_page = 2;

if (isset($_GET['classof']))
{
        // find out the number of results\records stored in the database

        $classof =  mysqli_real_escape_string($conn, $_GET['classof']);


        $query = "SELECT *  from register where classof=? order by firstname asc";
        $statement = $conn->prepare($query);
        $statement->bind_param('s', $classof);
        $statement->execute();
        $result = $statement->get_result();
        $number_of_results = $result->num_rows;
        $statement->close();

        // determine the number of total pages available
        $number_of_pages = ceil($number_of_results/$results_per_page);

         // determine which page number visitor is currently on
        if(!isset($_GET['page']))
        {
            $page=1;
        }
        else
        {
            $page = $_GET['page'];
        }



    //determine the sql limit starting number for the results on the displaying page
    $this_page_first_result = ($page-1)*$results_per_page;

if ($number_of_results > 0 )
    {
        //retrieve selected results from database and display them on page.
        $sql = "SELECT *  from register where classof=? order by firstname asc LIMIT " . $this_page_first_result . ',' . $results_per_page;

        $stmt = $conn->prepare($sql);
        $stmt->bind_param('s', $classof);
        $stmt->execute();
        $result = $stmt->get_result();
        $usercount = $result->num_rows;
        //$statement->close();


          echo "
          <center>
        <div class='container'>
        <div class='row'>
        <div class='col-md-6 offset-md-3 form-div'>
        <h3 class='text-success'> Class of $classof </h3>
        <table class='table table-striped table-borderless table-hover'>
            <thead class='thead-dark'>
                <tr>
                    <th >Firstname</th>
                    <th>Lastname</th>
                    <th>Country of Residence</th>
                    <th>Contact</th>
                </tr>
            </thead>";




        while ($row = mysqli_fetch_array($result))
        {

         $userid = $row["id"];
         $firstname = $row["firstname"];
         $lastname = $row["lastname"];
         $country = $row["country"];

         echo "
            <tr>
                <td>$firstname</td>
                <td>$lastname</td>
                <td>$country</td>
                <td><a href='contact_student.php?userid=$userid'>Contact</a></td>

            </tr>";



        }
    }



        echo "</table>";

         // Display the link to the pages
        for ($page=1;$page<=$number_of_pages;$page++)
        {
            //echo "
            //<tr>
            //";
            echo '<a href="students.php?page=' . $page . '">' . $page . '</a>';
            //echo "</tr>";
        }

        echo "
        </div>
        </div>
        </div>
        </center>";


}       



include ('includes/footer.php'); 
?>
$results\u per\u page=2;
如果(isset($_GET['classof']))
{
//找出数据库中存储的结果\记录数
$classof=mysqli\u real\u escape\u字符串($conn,$\u GET['classof']);
$query=“从寄存器中选择*,其中classof=?按名字asc排序”;
$statement=$conn->prepare($query);
$statement->bind_参数('s',$classof);
$statement->execute();
$result=$statement->get_result();
$number\u of_results=$result->num\u行;
$statement->close();
//确定可用的总页数
$number_of_pages=ceil($number_of_results/$results_per_pages);
//确定访问者当前所在的页码
如果(!isset($\u GET['page']))
{
$page=1;
}
其他的
{
$page=$_GET['page'];
}
//确定显示页面上结果的sql限制起始编号
$this_page_first_result=($page-1)*$results_每页;
如果($结果的数量>0)
{
//从数据库检索所选结果并将其显示在页面上。
$sql=“从寄存器中选择*,其中classof=?按名字排序asc限制“$this\u page\u first\u result.”,“$results\u per\u page;
$stmt=$conn->prepare($sql);
$stmt->bind_参数('s',$classof);
$stmt->execute();
$result=$stmt->get_result();
$usercount=$result->num\u行;
//$statement->close();
回声“
$classof类别
名字
姓氏
居住国
接触
";
while($row=mysqli\u fetch\u数组($result))
{
$userid=$row[“id”];
$firstname=$row[“firstname”];
$lastname=$row[“lastname”];
$country=$row[“country”];
回声“
$firstname
$lastname
$country
";
}
}
回声“;
//显示指向页面的链接
对于($page=1;$page)

在Sql中,分页需要使用限制和偏移量

选择
*
从…起
我的桌子
限制{PER_PAGE}偏移量{LAST_ELEMENT_NUMBER_IN_PREV_PAGE};

我已经能够通过更改脚本的以下部分来解决这个问题。所有其他部分都保持不变

之前:

变动后

请使用
ini_集('display_errors',1);ini_集('display_startup_errors',1);error_reporting(E_ALL);
我添加了它,但没有错误。我只是在单击页码时,没有显示结果集。我的sql查询具有在变量中定义的限制和偏移量。$sql=“从寄存器中选择*,其中classof=?按名字排序asc LIMIT”。“$this\u page\u first\u result.”,“$results\u per\u page;我发现您使用了短限制语法。请尝试使用
限制%n偏移量%n
,而不是
限制%n,%n