Home > php教程 > php手册 > PHP实现的oracle分页函数实例,phporacle分页实例

PHP实现的oracle分页函数实例,phporacle分页实例

WBOY
Release: 2016-06-13 08:46:55
Original
1071 people have browsed it

PHP实现的oracle分页函数实例,phporacle分页实例

本文实例讲述了PHP实现的oracle分页函数。分享给大家供大家参考,具体如下:

mysql有limit,分页的时候,我们可以用limit 30,40,而oracle没有limit,用其他的方法来代替--rownum.

一、底层方法

/**
 * 数据连接
 */
function connect($db_user, $db_pwd, $db_name, $db_nls)
{
 $this->dbh = oci_connect($db_user, $db_pwd, $db_name, $db_nls);
}
function getall($query, $start=0, $rows=-1)
{
 $this->dbh = emptyempty($this->dbh) ? $this->connect() : $this->dbh;
 $this->sth = oci_parse($this->dbh, $query);
 oci_execute($this->sth, OCI_DEFAULT) or $this->error($this->sth, $query);
 oci_fetch_all($this->sth, $res, $start, $rows, OCI_FETCHSTATEMENT_BY_ROW+OCI_ASSOC);
 return $res;
}
/**
* 获取分页数
*
* $query  string   查询语句
* $total  int    总条数
* $page  int    页数
* $rows  int    每页显示数
* @return integer  总行数
*/
function getpage($query, $total, $page, $rows=VAR_PAGENAV_ROWS)
{
 $start_row = ($page - 1) * $rows + 1;
 $start_row = $start_row > $total ? 1 : $start_row;
 $end_row  = $start_row + $rows - 1;
 $end_row  = $end_row > $total ? $total : $end_row;
 $query   = "SELECT * FROM ( SELECT ROWNUM as row_num, r.* FROM ( $query ) r WHERE ROWNUM <= $end_row ) WHERE $start_row <= row_num";
 return $this->getall($query);
}
/**
* 获取一个查询的总行数
*
* $string $sql 查询语句
*
* return integer 总行数
*/
function getcount($sql){
 $subSQL = 'select count(*) '.stristr($sql, 'from');
 return $this->getone($subSQL);
}
function getone($query)
{
 $row = $this->getrow($query);
 if (is_array($row))
 $one = current($row);
 else
 $one = '';
 unset($row);
 return $one;
}
function getrow($query)
{
 if (false === stripos($query, 'COUNT(')) {
 if (false === stripos($query, 'WHERE')) {
 $query .= ' WHERE ROWNUM = 1';
 } else {
 $query .= ' AND ROWNUM = 1';
 }
 }
 $result = $this->getall($query, 0, 1);
 if (emptyempty($result))
 $row = array();
 else
 $row = $result[0];
 unset($result);
 return $row;
}

Copy after login

主要二个方法我写一点注释

二、调用方法

$total = $this->db->getcount($sql);
$result = $this->db->getpage($sql,$total,$page,$page_number);

Copy after login

上面只是贴出部分代码,也是最核心的。

更多关于PHP数据库操作相关内容感兴趣的读者可查看本站专题:《php常见数据库操作技巧汇总》、《PHP基于pdo操作数据库技巧总结》及《php+mysql数据库操作入门教程》。

希望本文所述对大家PHP程序设计有所帮助。

您可能感兴趣的文章:

  • php+oracle 分页类
  • php连接oracle数据库及查询数据的方法
  • PHP处理Oracle的CLOB实例
  • Linux下PHP连接Oracle数据库
  • Win7 64位系统下PHP连接Oracle数据库
  • PHP 读取文本文件内容并分页显示
  • php自定义分页类完整实例
  • 两款万能的php分页类
  • php实现分页功能的3种方法
  • 详解PHP+AJAX无刷新分页实现方法
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template