PHP 在数据库中的分页表中排序

标签 php mysql database

我得到它来订购,但现在分页不起作用。现在没有任何内容打印到我的错误日志中。

用户 Red Acid 帮助我解决了缺少的空格并使用字符串变量而不是 PHP_SELF。

一旦我按下下一条记录,我得到:

Not Found

The requested URL /surf/$index.php was not found on this server.

Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.

我的代码:

<?php
                $dbHost = "localhost";
                $dbUser = "";
                $dbPass = "";
                $dbName = "";
                $tbl_name="";
                $rec_limit = 20;
                $order = "ORDER BY";
                $scriptname = 'index.php';

                mysql_connect("$dbHost", "$dbUser", "$dbPass")or die("cannot connect");
                mysql_select_db("$dbName")or die("cannot select DB");


                $sql="SELECT * FROM $tbl_name $order ".$_GET["orderby"]." ".$_GET["desc"];
                $result=mysql_query($sql);
                $sql = "SELECT count(steamid) FROM $tbl_name";
                    $result= mysql_query($sql);

                    if(! $result)
                    {
                       die('Could not get data: ' . mysql_error());
                    }
                $row = mysql_fetch_array($result, MYSQL_NUM );
                $rec_count = $row[0];

                if( isset($_GET{'page'} ) )
                {
                    $page = $_GET{'page'} + 1;
                    $offset = $rec_limit * $page ;
                }
                else
                {
                    $page = 0;
                    $offset = 0;
                }
                $left_rec = $rec_count - ($page * $rec_limit);
                $sql = "SELECT * ".
                "FROM $tbl_name ".
                "$order points DESC ".
                "LIMIT $offset, $rec_limit";
                $result=mysql_query($sql);
                while($rows=mysql_fetch_array($result)){

                ?>
                <tr>
                    <td><?php echo $rows['name']; ?></td>
                    <td><?php echo $rows['points']; ?></td>
                    <td><?php echo $rows['winratio']; ?></td>
                    <td><?php echo $rows['pointsratio']; ?></td>
                    <td><?php echo $rows['finishedmaps']; ?></td>
                    <td><?php echo $rows['lastseen']; ?></td>
                </tr>
                </tr>
                <?php
                }
                if( $page > 0 )
                     {
                        $last = $page - 2;
                        echo "<a href=\"$scriptname?page=$last\">Last 10 Records</a> |";
                        echo "<a href=\"$scriptname?page=$page\">Next 10 Records</a>";
                     }

                     else if( $page == 0 )
                     {
                        echo "<a href=\"$$scriptname?page=$page\">Next 10 Records</a>";
                        }

                     else if( $left_rec < $rec_limit )
                     {
                        $last = $page - 2;
                        echo "<a href=\"$$scriptname?page=$last\">Last 10 Records</a>";
                     }
                mysql_close();
                ?><?php
                $dbHost = "localhost";
                $dbUser = "c1226125_ms";
                $dbPass = "wR(@ucrb5oG.";
                $dbName = "c1226125_timer";
                $tbl_name="ck_playerrank";
                $rec_limit = 20;
                $order = "ORDER BY";
                $scriptname = 'index.php';

                mysql_connect("$dbHost", "$dbUser", "$dbPass")or die("cannot connect");
                mysql_select_db("$dbName")or die("cannot select DB");


                $sql="SELECT * FROM $tbl_name $order ".$_GET["orderby"]." ".$_GET["desc"];
                $result=mysql_query($sql);
                $sql = "SELECT count(steamid) FROM $tbl_name";
                    $result= mysql_query($sql);

                    if(! $result)
                    {
                       die('Could not get data: ' . mysql_error());
                    }
                $row = mysql_fetch_array($result, MYSQL_NUM );
                $rec_count = $row[0];

                if( isset($_GET{'page'} ) )
                {
                    $page = $_GET{'page'} + 1;
                    $offset = $rec_limit * $page ;
                }
                else
                {
                    $page = 0;
                    $offset = 0;
                }
                $left_rec = $rec_count - ($page * $rec_limit);
                $sql = "SELECT * ".
                "FROM $tbl_name ".
                "$order points DESC ".
                "LIMIT $offset, $rec_limit";
                $result=mysql_query($sql);
                while($rows=mysql_fetch_array($result)){

                ?>
                <tr>
                    <td><?php echo $rows['name']; ?></td>
                    <td><?php echo $rows['points']; ?></td>
                    <td><?php echo $rows['winratio']; ?></td>
                    <td><?php echo $rows['pointsratio']; ?></td>
                    <td><?php echo $rows['finishedmaps']; ?></td>
                    <td><?php echo $rows['lastseen']; ?></td>
                </tr>
                </tr>
                <?php
                }
                if( $page > 0 )
                     {
                        $last = $page - 2;
                        echo "<a href=\"$scriptname?page=$last\">Last 10 Records</a> |";
                        echo "<a href=\"$scriptname?page=$page\">Next 10 Records</a>";
                     }

                     else if( $page == 0 )
                     {
                        echo "<a href=\"$$scriptname?page=$page\">Next 10 Records</a>";
                        }

                     else if( $left_rec < $rec_limit )
                     {
                        $last = $page - 2;
                        echo "<a href=\"$$scriptname?page=$last\">Last 10 Records</a>";
                     }
                mysql_close();
                ?>

最佳答案

问题是 ORDER BY 和 DESC 之间没有空格:

$sql="SELECT * FROM $tbl_name ORDER BY ".$_GET["orderby"].$_GET["desc"];

应该是:

$sql="SELECT * FROM $tbl_name ORDER BY ".$_GET["orderby"]." ".$_GET["desc"];

使用字符串变量代替 PHP_SELF:

$scriptname = 'myscript.php'; // OR http://domain.com/myscript.php
echo "<a href=\"$scriptname?page=$last\">Last 10 Records</a>";

关于PHP 在数据库中的分页表中排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32676313/

相关文章:

php - 单击 href 后代码运行两次

php - 使用php中的位置 header 留在同一页面

android - Android 数据库操作的执行时间变化

用于调度通知的数据库(可能存储在 RAM 中 - radis、memcashedb)

php - 如何使用 Laravel Passport 在查询字符串而不是 header 中访问 token 来验证请求?

php - PHP 中的未定义索引和 MYSQL 中的数据类型 POINT

php - 如何使用mysql基于月份选择数据

php - 输出中获得的值未按正确的顺序排序

php - 从 MYSQL 查询填充文本框

python - 使用python google appengine获取数据库中每一行的行id/key