使用分页脚本的PDO语法或访问冲突错误

时间:2016-02-17 15:19:16

标签: php mysql pdo pagination

以下代码有效,但如果我输入某些组合,例如index.php?page_no(不在页面#)或page_no=0(零,但是page_no=1和以上所有内容),或者如果我在网址中输入超过19个数字(例如,22222222222222222222)(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);  
?>

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

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

我已经24小时全天候工作了好几天了。直到几周前我自己偶然发现了这个问题,我才知道自己有这个问题。

如果有人在这里阅读可以指引我朝着正确的方向前进,我将非常感激。谢谢!

1 个答案:

答案 0 :(得分:2)

乍一看,它看起来像是同一问题的两个方面:SQL&#39; LIMIT&#39;命令。

在一端,您提供负值:

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

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

当&#39; page_no&#39;为0,则限制最终为&#34; LIMIT -5,5&#34;,这是无效的。要解决此问题,请改用以下内容:

$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注入(某人恶意可以请求&#34;&#39; DROP TABLES&# 34;或类似于&#39; page_no&#39;变量并为您带来问题。)