PDO分页错误

时间:2015-04-16 17:43:17

标签: php mysql pdo pagination

我一直在使用pdo进行分页,但我似乎无法超越这一部分。我已经查找了其他帖子和教程,但我撞到了墙,有人可以帮忙吗? **编辑完整代码已添加,已添加更新但仍有错误

<?php
try{
    $handler = new PDO('mysql:host=127.0.0.1;dbname=clients','root','');
    $handler->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e){
    echo $e->getMessage();
    die();
}


?>
<html>
<head>
<link rel="stylesheet" type="text/css" href="mystyle.css">
</head>


<body bgcolor="#F5F1DE">

<div class="center">
  <br>
   <br>
    <br>
     <br>
      <br>
  <h2> Business Listings</h2>


<table  border="1" style="width:50%">
    <tr>
<?php
$per_page = 10;
$NoofRecords = $handler->query('SELECT COUNT(CompID) FROM Listings;');
$pages = ceil($NoofRecords / $per_page);

if(!isset($_GET['page'])){
    header(":location: Listings.php?page=1");
}
else{
    $page =$_GET['page'];
}

$start = (($page - 1)*$per_page);
echo "SELECT * FROM listings WHERE Accept=1 ORDER BY Premium LIMIT                  $start,$per_page;";
$query = $handler->query("SELECT * FROM listings WHERE Accept=1 ORDER BY Premium LIMIT $start,$per_page;");
while($r = $query->fetch()) {
    echo '<td>', $r['Name'], '</td>','<td>', $r['Email'], '</td>','<td>',         $r['Phone'], '</td>',
'<td>','<a href="' . $r['Name'] . '.php">Company page</a>', '</td>';
echo '</tr>';
}

for($number=1;$number<=$pages;$number++)
{
    echo '<a href="?page'.$number.'">.$number. </a>';
}   
echo"<BR>Current Page: $page"
?>

</table>
  </Div>
</div>


</div>
</body>
</html>

4 个答案:

答案 0 :(得分:1)

在字符串中使用变量时,请使用双引号"而不是单引号'

"SELECT * FROM listings WHERE Accept=1 ORDER BY Premium LIMIT $start,$per_page;"

答案 1 :(得分:0)

限制在您的查询中排在最后。

SELECT * FROM listings WHERE Accept=1 ORDER BY Premium LIMIT $start,$per_page ;

https://dev.mysql.com/doc/refman/5.0/en/select.html

你还需要使用双引号,因为Crystal指出PHP可以处理变量。你应该使用准备好的陈述,虽然它可以打针,How can I prevent SQL injection in PHP?

更新后,没有准备好的陈述你还应该使用..

<?php
try{
    $handler = new PDO('mysql:host=127.0.0.1;dbname=clients','root','');
    $handler->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e){
    echo $e->getMessage();
    die();
}
$per_page = 10;
$NoofRecords = $handler->query('SELECT COUNT(CompID) as counted FROM Listings;');
$fetchdata = $NoofRecords->fetch();
$recordcount = $fetchdata['counted'];
$pages = ceil($recordcount / $per_page);
if(empty($_GET['page'])){
    //this is a waste if this page is listings.php, just set $page to 1;
    header("Location: Listings.php?page=1");
} else{
    $page = $_GET['page'];
}
$start = (($page - 1)*$per_page);
echo "SELECT * FROM listings WHERE Accept=1 ORDER BY Premium LIMIT $start,$per_page;";
$query = $handler->query("SELECT * FROM listings WHERE Accept=1 ORDER BY Premium LIMIT $start,$per_page;");
while($r = $query->fetch()) {
    $table_contents = '<tr><td>' . $r['Name'] . '</td><td>' . $r['Email'] . '</td><td>'. $r['Phone'] . '</td><td><a href="' . $r['Name'] . '.php">Company page</a></td></tr>';
}
?>
<html>
<head>
<link rel="stylesheet" type="text/css" href="mystyle.css">
</head>
<body bgcolor="#F5F1DE">
<div class="center"> 
    <br>
    <br>
    <br>
    <br>
    <br>
    <h2> Business Listings</h2>
    <table  border="1" style="width:50%"> 
        <?php echo $table_contents; ?>
    </table>
    <?php
    for($number=1;$number<=$pages;$number++) {
        echo '<a href="?page'.$number.'">'.$number. '</a>';
    }   
    echo "<BR>Current Page: $page"; ?>

</div>
</div><!-- I dont know what these 2 divs are for... -->
</div> 
</body>
</html>

答案 2 :(得分:0)

将您的查询行更改为:查询语法错误。

$query = $handler->query("SELECT * FROM listings WHERE Accept=1 ORDER BY Premium LIMIT $start,$per_page");

答案 3 :(得分:0)

除了其他答案中提到的sql问题(最后为LIMIT,双引号以便你可以在字符串中使用变量),你已经遇到了一个问题:

$pages = ceil(($NoofRecords,0)/$per_page);
              ^^^^^^^^^^^^^^^^ What is supposed to happen here?

,0部分使整个声明无效。

你只需要:

$pages = ceil($NoofRecords / $per_page);