如何用PHP实现从PostgreSQL数据库分页查询并显示数据?
- 内容介绍
- 文章标签
- 相关推荐
本文共计1567个文字,预计阅读时间需要7分钟。
本文实例讲述了PHP实现从PostgreSQL数据库检索数据、分页显示及根据条件查找数据的方法。分享给广大读者,仅供参考。
主要功能是从PostgreSQL查询数据,并进行搜索和分页显示。由于刚刚开始接触,以下为简要介绍:
1. 从PostgreSQL查询数据: - 使用PHP的PDO(PHP Data Objects)扩展连接到PostgreSQL数据库。 - 通过SQL查询语句获取所需数据。
2. 搜索功能: - 在查询语句中添加WHERE条件,根据用户输入的搜索关键字进行过滤。
3. 分页显示: - 在查询语句中添加LIMIT和OFFSET子句,实现分页功能。 - 根据当前页码计算OFFSET值。
示例代码如下:
php
$dsn=pgsql:host=$host;dbname=$dbname;charset=$charset;$options=[ PDO::ATTR_ERRMODE=> PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE=> PDO::FETCH_ASSOC, PDO::ATTR_EMULATE_PREPARES=> false,];
try { $pdo=new PDO($dsn, $user, $pass, $options);} catch (\PDOException $e) { throw new \PDOException($e->getMessage(), (int)$e->getCode());}
// 获取当前页码$page=isset($_GET['page']) ? (int)$_GET['page'] : 1;$pageSize=10; // 每页显示10条数据$offset=($page - 1) * $pageSize;
// 搜索关键字$searchKeyword=isset($_GET['search']) ? $_GET['search'] : '';
// 查询语句$sql=SELECT * FROM your_table WHERE your_column LIKE :searchKeyword LIMIT :pageSize OFFSET :offset;
// 准备语句$stmt=$pdo->prepare($sql);
// 绑定参数$stmt->bindParam(':searchKeyword', $searchKeyword . '%');$stmt->bindParam(':pageSize', $pageSize, PDO::PARAM_INT);$stmt->bindParam(':offset', $offset, PDO::PARAM_INT);
// 执行查询$stmt->execute();
// 获取查询结果$results=$stmt->fetchAll();
// 显示数据foreach ($results as $row) { echo $row['your_column'] . '';}?>
以上代码仅为示例,实际应用中需要根据具体需求进行调整。希望对您有所帮助。
本文实例讲述了PHP实现从PostgreSQL数据库检索数据分页显示及根据条件查找数据。分享给大家供大家参考,具体如下:
主要功能是从postgreSql查询数据,并检索,由于自己刚开始接触,所以难点在于多条数据同时筛选并分页显示出来,写下自己的代码与大家共享。
<html> <head> <script type="text/javascript"> /** * 分页函数 * pno--页数 * psize--每页显示记录数 * 分页部分是从真实数据行开始,因而存在加减某个常数,以确定真正的记录数 * 纯js分页实质是数据行全部加载,通过是否显示属性完成分页功能 **/ function goPage(pno,psize){ var itable = document.getElementById("idData"); var num = itable.rows.length;//表格所有行数(所有记录数) console.log(num); var totalPage = 0;//总页数 var pageSize = psize;//每页显示行数 //总共分几页 if(num/pageSize > parseInt(num/pageSize)){ totalPage=parseInt(num/pageSize)+1; }else{ totalPage=parseInt(num/pageSize); } var currentPage = pno;//当前页数 var startRow = (currentPage - 1) * pageSize+1;//开始显示的行 31 var endRow = currentPage * pageSize;//结束显示的行 40 endRow = (endRow > num)? num : endRow; 40 console.log(endRow); //遍历显示数据实现分页 for(var i=1;i<(num+1);i++){ var irow = itable.rows[i-1]; if(i>=startRow && i<=endRow){ irow.style.display = "block"; }else{ irow.style.display = "none"; } } var pageEnd = document.getElementById("pageEnd"); var tempStr = "共"+num+"条记录 分"+totalPage+"页 当前第"+currentPage+"页"; if(currentPage>1){ tempStr += "<a href=\"#\" onClick=\"goPage("+(1)+","+psize+")\">首页</a>"; tempStr += "<a href=\"#\" onClick=\"goPage("+(currentPage-1)+","+psize+")\"><上一页</a>" }else{ tempStr += "首页"; tempStr += "<上一页"; } if(currentPage<totalPage){ tempStr += "<a href=\"#\" onClick=\"goPage("+(currentPage+1)+","+psize+")\">下一页></a>"; tempStr += "<a href=\"#\" onClick=\"goPage("+(totalPage)+","+psize+")\">尾页</a>"; }else{ tempStr += "下一页>"; tempStr += "尾页"; } document.getElementById("barcon").innerHTML = tempStr; } </script> <style type="text/css"> table { text-align:center; width:1000px; } th { width:100px; } input { width:100px; } td { width:100px; } </style> </head> <body onLoad="goPage(1,10);"> <form method="post" action="browes.php"> <table border="1"> <tr> <th><h2>个人概况一览</h2></th> </tr> <tr> <table border="1" > <tr> <td>姓:</td> <td><input type="text" name="surname"> </td> <td>名:</td> <td><input type="text" name="name"> </td> <td>手机:</td> <td><input type="text" name="phone"> </td> <td>性别:</td> <td><select name="sex" id="select_k1" class="xla_k"> <option value=""> </option> <option value="male">男</option> <option value="female">女</option> </select> </td> <td>学校:</td> <td><input type="text" name="school"> </td> </tr> </table> </tr> <tr> <td><input type="submit" name="submit" value="提交"> </td> <td><input name=reset type=reset value=重置></td> </tr> </table> </form> </body> <body> <table id="idData" border="1" > <tr> <th>照片</th> <th>姓名</th> <th>性别</th> <th>生日</th> <th>邮箱</th> <th>电话</th> <th>学校</th> <th>技能</th> <th>选项</th> </tr> <?php include "../head.php"; $s = $_POST["surname"]; $a = $_POST["name"]; $b = $_POST["phone"]; $c = $_POST["sex"]; $d = $_POST["school"]; /* 下面这段代码是PostgreSQL数据库多条数据检索编写数据库的通用方法 */ $field = "where 1 = 1 "; if($a){ //magic_quotes_gpc=on,addslashes not used. $name = str_replace('\'', "''", $a); $field.= "and (name like '%".$name."%') "; } if(($s)!=NULL){ $surname = str_replace('\'', "''", $s); $field.= "and (surname like '%".$surname."%') "; } if(($c)!=NULL){ $field.= "and (sex = '".$c."') "; } if(($d)!=NULL){ $school = str_replace('\'', "''", $d); $field.= "and (school like '%".$school."%') "; } if(($b)!=NULL){ $tel = str_replace('\'', "''", $b); $field.= "and (phone = '".$tel."') "; } $sql = "select * from worker ".$field; /* 上面这段代码是PostgreSQL数据库多条数据检索编写数据库的通用方法 */ $ret = pg_query($db, $sql); while($row=pg_fetch_row($ret)){ ?> <tr> <td><?php echo $row[9];?></td> <td><?php echo $row[1].$row[2];?></td> <td><?php echo $row[3];?></td> <td><?php echo $row[4];?></td> <td><?php echo $row[5];?></td> <td><?php echo $row[6];?></td> <td><?php echo $row[7];?></td> <td><?php echo $row[8];?></td> <td><button><a href = "<?php echo 'change.php?id='.$row[0] ?>">change</button> <button><a href = "<?php echo 'delete.php?id='.$row[0] ?>">delete</button></td> </tr> <?php } ?> </table> <table > <div id="barcon" name="barcon"></div> </table> </body> </html>
更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP基于pdo操作数据库技巧总结》、《php+Oracle数据库程序设计技巧总结》、《PHP+MongoDB数据库操作技巧大全》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家PHP程序设计有所帮助。
本文共计1567个文字,预计阅读时间需要7分钟。
本文实例讲述了PHP实现从PostgreSQL数据库检索数据、分页显示及根据条件查找数据的方法。分享给广大读者,仅供参考。
主要功能是从PostgreSQL查询数据,并进行搜索和分页显示。由于刚刚开始接触,以下为简要介绍:
1. 从PostgreSQL查询数据: - 使用PHP的PDO(PHP Data Objects)扩展连接到PostgreSQL数据库。 - 通过SQL查询语句获取所需数据。
2. 搜索功能: - 在查询语句中添加WHERE条件,根据用户输入的搜索关键字进行过滤。
3. 分页显示: - 在查询语句中添加LIMIT和OFFSET子句,实现分页功能。 - 根据当前页码计算OFFSET值。
示例代码如下:
php
$dsn=pgsql:host=$host;dbname=$dbname;charset=$charset;$options=[ PDO::ATTR_ERRMODE=> PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE=> PDO::FETCH_ASSOC, PDO::ATTR_EMULATE_PREPARES=> false,];
try { $pdo=new PDO($dsn, $user, $pass, $options);} catch (\PDOException $e) { throw new \PDOException($e->getMessage(), (int)$e->getCode());}
// 获取当前页码$page=isset($_GET['page']) ? (int)$_GET['page'] : 1;$pageSize=10; // 每页显示10条数据$offset=($page - 1) * $pageSize;
// 搜索关键字$searchKeyword=isset($_GET['search']) ? $_GET['search'] : '';
// 查询语句$sql=SELECT * FROM your_table WHERE your_column LIKE :searchKeyword LIMIT :pageSize OFFSET :offset;
// 准备语句$stmt=$pdo->prepare($sql);
// 绑定参数$stmt->bindParam(':searchKeyword', $searchKeyword . '%');$stmt->bindParam(':pageSize', $pageSize, PDO::PARAM_INT);$stmt->bindParam(':offset', $offset, PDO::PARAM_INT);
// 执行查询$stmt->execute();
// 获取查询结果$results=$stmt->fetchAll();
// 显示数据foreach ($results as $row) { echo $row['your_column'] . '';}?>
以上代码仅为示例,实际应用中需要根据具体需求进行调整。希望对您有所帮助。
本文实例讲述了PHP实现从PostgreSQL数据库检索数据分页显示及根据条件查找数据。分享给大家供大家参考,具体如下:
主要功能是从postgreSql查询数据,并检索,由于自己刚开始接触,所以难点在于多条数据同时筛选并分页显示出来,写下自己的代码与大家共享。
<html> <head> <script type="text/javascript"> /** * 分页函数 * pno--页数 * psize--每页显示记录数 * 分页部分是从真实数据行开始,因而存在加减某个常数,以确定真正的记录数 * 纯js分页实质是数据行全部加载,通过是否显示属性完成分页功能 **/ function goPage(pno,psize){ var itable = document.getElementById("idData"); var num = itable.rows.length;//表格所有行数(所有记录数) console.log(num); var totalPage = 0;//总页数 var pageSize = psize;//每页显示行数 //总共分几页 if(num/pageSize > parseInt(num/pageSize)){ totalPage=parseInt(num/pageSize)+1; }else{ totalPage=parseInt(num/pageSize); } var currentPage = pno;//当前页数 var startRow = (currentPage - 1) * pageSize+1;//开始显示的行 31 var endRow = currentPage * pageSize;//结束显示的行 40 endRow = (endRow > num)? num : endRow; 40 console.log(endRow); //遍历显示数据实现分页 for(var i=1;i<(num+1);i++){ var irow = itable.rows[i-1]; if(i>=startRow && i<=endRow){ irow.style.display = "block"; }else{ irow.style.display = "none"; } } var pageEnd = document.getElementById("pageEnd"); var tempStr = "共"+num+"条记录 分"+totalPage+"页 当前第"+currentPage+"页"; if(currentPage>1){ tempStr += "<a href=\"#\" onClick=\"goPage("+(1)+","+psize+")\">首页</a>"; tempStr += "<a href=\"#\" onClick=\"goPage("+(currentPage-1)+","+psize+")\"><上一页</a>" }else{ tempStr += "首页"; tempStr += "<上一页"; } if(currentPage<totalPage){ tempStr += "<a href=\"#\" onClick=\"goPage("+(currentPage+1)+","+psize+")\">下一页></a>"; tempStr += "<a href=\"#\" onClick=\"goPage("+(totalPage)+","+psize+")\">尾页</a>"; }else{ tempStr += "下一页>"; tempStr += "尾页"; } document.getElementById("barcon").innerHTML = tempStr; } </script> <style type="text/css"> table { text-align:center; width:1000px; } th { width:100px; } input { width:100px; } td { width:100px; } </style> </head> <body onLoad="goPage(1,10);"> <form method="post" action="browes.php"> <table border="1"> <tr> <th><h2>个人概况一览</h2></th> </tr> <tr> <table border="1" > <tr> <td>姓:</td> <td><input type="text" name="surname"> </td> <td>名:</td> <td><input type="text" name="name"> </td> <td>手机:</td> <td><input type="text" name="phone"> </td> <td>性别:</td> <td><select name="sex" id="select_k1" class="xla_k"> <option value=""> </option> <option value="male">男</option> <option value="female">女</option> </select> </td> <td>学校:</td> <td><input type="text" name="school"> </td> </tr> </table> </tr> <tr> <td><input type="submit" name="submit" value="提交"> </td> <td><input name=reset type=reset value=重置></td> </tr> </table> </form> </body> <body> <table id="idData" border="1" > <tr> <th>照片</th> <th>姓名</th> <th>性别</th> <th>生日</th> <th>邮箱</th> <th>电话</th> <th>学校</th> <th>技能</th> <th>选项</th> </tr> <?php include "../head.php"; $s = $_POST["surname"]; $a = $_POST["name"]; $b = $_POST["phone"]; $c = $_POST["sex"]; $d = $_POST["school"]; /* 下面这段代码是PostgreSQL数据库多条数据检索编写数据库的通用方法 */ $field = "where 1 = 1 "; if($a){ //magic_quotes_gpc=on,addslashes not used. $name = str_replace('\'', "''", $a); $field.= "and (name like '%".$name."%') "; } if(($s)!=NULL){ $surname = str_replace('\'', "''", $s); $field.= "and (surname like '%".$surname."%') "; } if(($c)!=NULL){ $field.= "and (sex = '".$c."') "; } if(($d)!=NULL){ $school = str_replace('\'', "''", $d); $field.= "and (school like '%".$school."%') "; } if(($b)!=NULL){ $tel = str_replace('\'', "''", $b); $field.= "and (phone = '".$tel."') "; } $sql = "select * from worker ".$field; /* 上面这段代码是PostgreSQL数据库多条数据检索编写数据库的通用方法 */ $ret = pg_query($db, $sql); while($row=pg_fetch_row($ret)){ ?> <tr> <td><?php echo $row[9];?></td> <td><?php echo $row[1].$row[2];?></td> <td><?php echo $row[3];?></td> <td><?php echo $row[4];?></td> <td><?php echo $row[5];?></td> <td><?php echo $row[6];?></td> <td><?php echo $row[7];?></td> <td><?php echo $row[8];?></td> <td><button><a href = "<?php echo 'change.php?id='.$row[0] ?>">change</button> <button><a href = "<?php echo 'delete.php?id='.$row[0] ?>">delete</button></td> </tr> <?php } ?> </table> <table > <div id="barcon" name="barcon"></div> </table> </body> </html>
更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP基于pdo操作数据库技巧总结》、《php+Oracle数据库程序设计技巧总结》、《PHP+MongoDB数据库操作技巧大全》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家PHP程序设计有所帮助。

