php - 分页脚本的 PDO 语法或访问冲突错误

标签 php mysql pdo pagination

下面的代码有效,但如果我输入一些组合,例如 index.php?page_no(没有页面 #)或 page_no=0(即零,但 page_no=1 和上面的所有内容都有效),或者如果我在 url 中输入超过 19 个数字(例如,222222222222222222222)(在 index.php?page_no=) 之后我得到以下类型的错误:

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '5.55555555556E+19,5' at line 1' in

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '-5,5' at line 1' in

我的代码如下:

分页:

<?php
class pager {
    private $db;

    function __construct($DB_con) {
        $this->db = $DB_con;
    }

    public function dataview($query)
    {
        $stmt = $this->db->prepare($query);
        $stmt->bindParam(passport, $_GET['passport'], PDO::PARAM_INT);
        $stmt->execute();
        if ($stmt->rowCount() > 0) {
            while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
                ?>

                // some html here and some echo of columns

                <?php
            }
        }
    }

    public function pagers($query,$records_per_page) {
        $starting_position=0;
        if(isset($_GET["page_no"])) {
            $starting_position=($_GET["page_no"]-1)*$records_per_page;
        }
        $query2=$query." limit $starting_position,$records_per_page";
        return $query2;
    }

    public function pagerslink($query,$records_per_page)
    {
        $self = $_SERVER['PHP_SELF'];
        $stmt = $this->db->prepare($query);
        $stmt->bindParam(passport, $_GET['passport'], PDO::PARAM_INT);
        $stmt->execute();
        $total_no_of_records = $stmt->rowCount();
        if($total_no_of_records > 0)
        {
            ?><tr><td colspan="7"><?php
            $total_no_of_pages=ceil($total_no_of_records/$records_per_page);
            $current_page=1;
            if(isset($_GET["page_no"]))
            {
                $current_page=$_GET["page_no"];
            }
            if($current_page!=1) {
                $previous =$current_page-1;
                echo "<a href='".$self."?page_no=1'>First</a>&nbsp;&nbsp;";
                echo "<a href='".$self."?page_no=".$previous."'>Previous</a>&nbsp;&nbsp;";
            }
            $x="";
            for($i=1;$i<=$total_no_of_pages;$i++) {
                if($i==$current_page) {
                    $x.= "<strong><a href='".$self."?page_no=".$i."' 
style='color:red;text-decoration:none'>".$i."</a></strong>&nbsp;&nbsp;";
                }
                elseif ($i>6 && $i!=$total_no_of_pages) {
                    $x.= ".";
                }
                else {
                    $x.= "<a href='".$self."?page_no=".$i."'>".$i."</a>&nbsp;&nbsp;";
                }
            }
            echo $x;
            if($current_page!=$total_no_of_pages)
            {
                $next=$current_page+1;
                echo "<a href='".$self."?page_no=".$next."'>Next</a>&nbsp;&nbsp;";
                echo
                    "<a href='".$self."page_no=".$total_no_of_pages."'>Last</a>&nbsp;&nbsp;";
            }
        }
    }
}

索引:

<?php
$query = "SELECT * FROM view-i-created ORDER BY passport DESC";
$records_per_page = 5;
$newquery = $pager->pagers($query,$records_per_page);
$pager->dataview($newquery);
$pager->pagerslink($query,$records_per_page);  
?>

我的 Config 文件的一部分(我最近添加了第一行,但没有帮助):

$DB_con->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$DB_con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

几天来,我一直在全天候(7 天 24 小时)工作。直到几周前我自己偶然发现了这个问题,我才知道我遇到了这个问题。

如果有人阅读这里可以指导我正确的方向,我将非常感激。谢谢!

最佳答案

乍一看,它看起来像是同一个问题的两个方面:SQL“LIMIT”命令的参数无效。

一方面,您提供负值:

$starting_position=($_GET["page_no"]-1)*$records_per_page;

$query2=$query." limit $starting_position,$records_per_page";

当 'page_no' 为 0 时,限制最终为“LIMIT -5,5”,这是无效的。要解决此问题,请改用如下内容:

$limit_bounded = min(max(0, $starting_position), PHP_INT_MAX); // Bounded between 0 and PHP_INT_MAX, which is ~2147000000.
$query2=$query." limit $limit_bounded,$records_per_page";

在高端,您可能会看到整数溢出,或者只是对限制的上限使用了太多字符。

作为一个安全问题,你应该转义你在 SQL 查询中使用的参数,特别是当你从 $_GET 接收它们时,以防止 SQL 注入(inject)(恶意的人可能会请求“'DROP TABLES”或类似的'page_no '变量并给你带来问题。)

关于php - 分页脚本的 PDO 语法或访问冲突错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35460661/

相关文章:

PHP 事务提交正常但 MySQL 数据库未更改

php - PHP 中的 shell_exec 返回空字符串

java - com.mysql.jdbc.exceptions.jdbc4.CommunicationsException : Communications link failure Software caused connection abort: recv failed

mysql - 导致 mysql 抛出错误的设置

php - mysql LIMIT 和 PDO 的问题

php - pdo 使用 avg 列出前 5 名?

php - 为什么我的 JSON 对象的 AJAX 调用返回其特定内容未定义?

php - 如何根据 PHP 中选定的百分比返回数组的一部分?

php - mysql_fetch_array()/mysql_fetch_assoc()/mysql_fetch_row()/mysql_num_rows 等...期望参数 1 是资源

java - 如何减少部分冲洗