PHP 分页与 PDO 不起作用

标签 php mysql pdo pagination

我正在尝试进行 php 分页,但出现以下错误:

Notice: Undefined variable: Table in C:\wamp64\www\pagination\pagination\function.php on line 10

Fatal error: Call to a member function makeStatement() on null in C:\wamp64\www\pagination\pagination\function.php on line 10

我的代码分为 2 页 - paging.php 和 function.php

分页.PHP

<?php
$dbInfo = "mysql:host=localhost;dbname=paging";
$dbUser = "root";
$dbPassword = "password";
$db = new PDO( $dbInfo, $dbUser, $dbPassword );
$db->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
?>
<!DOCTYPE html>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="pagination/css/pagination.css" rel="stylesheet" type="text/css" />
<link href="pagination/css/A_green.css" rel="stylesheet" type="text/css" />
</head>
<body>
<?php 
include("pagination/function.php");
$Table = new Table($db);

$page = (int) (!isset($_GET["page"]) ? 1 : $_GET["page"]);
    $limit = 5; //if you want to dispaly 10 records per page then you have to change here
    $startpoint = ($page * $limit) - $limit;
    $statement = "table1 order by name asc"; //you have to pass your query over here

    $entrySQL= "select * from {$statement} LIMIT {$startpoint} , {$limit}";

    $res = $Table->makeStatement( $entrySQL );

    while($row = $res->fetch(PDO::FETCH_ASSOC))
    {
    echo $row["name"];
    echo "<br>";
    }

    class Table {
        //notice protected, not private
        public $db;
        
        public function __construct ( $db ) {
            $this->db = $db;
        }

        //notice protected, not private  
        //$sql argument must be an SQL string
        //$data must be an array of dynamic data to use in the SQL 
        public function makeStatement ( $sql, $data = null ){
            $statement = $this->db->prepare( $sql );
            try {
                $statement->execute( $data );
            } catch (Exception $e) {
                $exceptionMessage = "<p>You tried to run this sql: $sql <p>
                        <p>Exception: $e</p>";
                trigger_error($exceptionMessage);
            }       
            return $statement;
        }   
    }
 ?>

<?php
echo "<div id='pagingg' >";
echo pagination($statement,$limit,$page);
echo "</div>";
?>
</body>
</html>

函数.PHP

<?php
    function pagination($query, $per_page = 10,$page = 1, $url = '?'){        
    $query = "SELECT COUNT(*) as `num` FROM $query";

    $res = $Table->makeStatement( $query );

    $row = $res->fetch(PDO::FETCH_ASSOC);


    $total = $row['num'];
    $adjacents = "2"; 

    $page = ($page == 0 ? 1 : $page);  
    $start = ($page - 1) * $per_page;                               
    
    $prev = $page - 1;                          
    $next = $page + 1;
    $lastpage = ceil($total/$per_page);
    $lpm1 = $lastpage - 1;
    
    $pagination = "";
    if($lastpage > 1)
    {   
        $pagination .= "<ul class='pagination'>";
                $pagination .= "<li class='details' style='margin-top:2px'>Page $page of $lastpage</li>";
        if ($lastpage < 7 + ($adjacents * 2))
        {   
            for ($counter = 1; $counter <= $lastpage; $counter++)
            {
                if ($counter == $page)
                    $pagination.= "<li><a class='current'>$counter</a></li>";
                else
                    $pagination.= "<li><a href='{$url}page=$counter'>$counter</a></li>";                    
            }
        }
        elseif($lastpage > 5 + ($adjacents * 2))
        {
            if($page < 1 + ($adjacents * 2))        
            {
                for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
                {
                    if ($counter == $page)
                        $pagination.= "<li><a class='current'>$counter</a></li>";
                    else
                        $pagination.= "<li><a href='{$url}page=$counter'>$counter</a></li>";                    
                }
                $pagination.= "<li class='dot'>...</li>";
                $pagination.= "<li><a href='{$url}page=$lpm1'>$lpm1</a></li>";
                $pagination.= "<li><a href='{$url}page=$lastpage'>$lastpage</a></li>";      
            }
            elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
            {
                $pagination.= "<li><a href='{$url}page=1'>1</a></li>";
                $pagination.= "<li><a href='{$url}page=2'>2</a></li>";
                $pagination.= "<li class='dot'>...</li>";
                for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
                {
                    if ($counter == $page)
                        $pagination.= "<li><a class='current'>$counter</a></li>";
                    else
                        $pagination.= "<li><a href='{$url}page=$counter'>$counter</a></li>";                    
                }
                $pagination.= "<li class='dot'>..</li>";
                $pagination.= "<li><a href='{$url}page=$lpm1'>$lpm1</a></li>";
                $pagination.= "<li><a href='{$url}page=$lastpage'>$lastpage</a></li>";      
            }
            else
            {
                $pagination.= "<li><a href='{$url}page=1'>1</a></li>";
                $pagination.= "<li><a href='{$url}page=2'>2</a></li>";
                $pagination.= "<li class='dot'>..</li>";
                for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
                {
                    if ($counter == $page)
                        $pagination.= "<li><a class='current'>$counter</a></li>";
                    else
                        $pagination.= "<li><a href='{$url}page=$counter'>$counter</a></li>";                    
                }
            }
        }
        
        if ($page < $counter - 1){ 
            $pagination.= "<li><a href='{$url}page=$next'>Next</a></li>";
            $pagination.= "<li><a href='{$url}page=$lastpage'>Last</a></li>";
        }else{
            $pagination.= "<li><a class='current'>Next</a></li>";
            $pagination.= "<li><a class='current'>Last</a></li>";
        }
        $pagination.= "</ul>\n";        
    }


    return $pagination;
   } 
?>

78

最佳答案

PHP functions have a local variable scope 。因此,pagination 函数内部的 $Table 变量不会引用实例化对象的全局变量 $Table

相反,您应该在像这样调用函数时将全局变量传递到函数中......

echo pagination($Table, $statement, $limit, $page);

并确保更新您的函数签名,以便它将 Table 对象作为依赖项。

function pagination(Table $Table, $query, $per_page = 10, $page = 1, $url = '?')

关于PHP 分页与 PDO 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38943227/

相关文章:

php - Preg 匹配方括号和 @strings 内的字符串

php - 如何使用PHP的爆炸功能并将值插入到MYSQL中

mysql - 使用 SQL,如何编写一个查询,如果没有条目则返回 null,但如果存在则列出所有条目?

php - PDO 更新 mysql 字段

PHP PDO 准备语句——MySQL LIKE 查询

php - 在多开发 nginx 设置中保护密码

php - 尝试在 PHP 中使用 XBase 库时找不到类错误

mysql - 黑客排名 : LIKE vs REGEXP

mysql - MySQL中将n条记录合并为一条记录

php - 当测试外部Mysql连接时,我得到CDbConnection failed to open the DB connection