Home >Web Front-end >JS Tutorial >Implement PDO paging and url rewriting
PDO (PHP Data Objects) defines a lightweight unified interface in PHP that can be used to access different types of databases.
Requirements
Write code to add a piece of data, PDO into the database (the title cannot be repeated
Paging function is implemented, page link Write a regex for list_page number.html
(regular link address) Create a new .htaccess with the next content
Displayed code
<?php /** * Created by PhpStorm. * User: admin * Date: 2018/9/14 * Time: 8:57 */ $page = empty($_GET['page'])?1:$_GET['page']; $pdo = new PDO('mysql:host=localhost;dbname=musicl;charset=utf8;','root','root'); $sql = "select count(*) from text"; $count = $pdo->query($sql)->fetchAll(); //条数 $count = $count[0]['count(*)']; $limit = 3; //总页数 $numpage = ceil($count/$limit); //偏移量 $last = ($page-1)*$limit; if($page<1) { $page=1; return false; } if($page>$numpage) { $page=$numpage; return false; } $sql = "select * from text limit $last,$limit"; $data=$pdo->query($sql)->fetchAll(PDO::FETCH_ASSOC); ?> <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <table border="1"> <tr> <td>标题</td> <td>作者</td> <td>链接</td> <td>内容</td> <td>时间</td> </tr> <?php foreach($data as $v){?> <tr> <td><?php echo $v['name']?></td> <td><?php echo $v['author']?></td> <td><?php echo $v['link']?></td> <td><?php echo $v['content']?></td> <td><?php echo $v['time']?></td> </tr> <?php }?> </table> <a href="list_<?php echo $page-1?>.html">上一页</a> <a href="list_<?php echo $page+1?>.html">下一页</a> </body> </html>
Related recommendations:
php pdo insert and pdo insertId usage
The above is the detailed content of Implement PDO paging and url rewriting. For more information, please follow other related articles on the PHP Chinese website!