Home  >  Article  >  Backend Development  >  Example of oracle paging function implemented in PHP, phporacle paging example_PHP tutorial

Example of oracle paging function implemented in PHP, phporacle paging example_PHP tutorial

WBOY
WBOYOriginal
2016-07-12 08:59:57957browse

Oracle paging function example implemented by PHP, phporacle paging example

The example in this article describes the oracle paging function implemented by PHP. Share it with everyone for your reference, the details are as follows:

MySQL has a limit. When paging, we can use limit 30, 40, but Oracle does not have a limit. Use other methods instead - rownum.

1. Underlying methods

/**
 * 数据连接
 */
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;
}

I will write some comments on the two main methods

2. Calling method

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

Only part of the code is posted above, which is also the core.

Readers who are interested in more content related to PHP database operations can check out the special topics of this site: "Summary of Common Database Operation Skills in PHP", "Summary of PHP Database Operation Skills Based on PDO" and "Introduction Tutorial on PHP MySQL Database Operation".

I hope this article will be helpful to everyone in PHP programming.

Articles you may be interested in:

  • php oracle paging class
  • How to connect Oracle database and query data with PHP
  • PHP handles Oracle's CLOB instance
  • PHP under Linux connects to Oracle database
  • PHP under Win7 64-bit system connects to Oracle database
  • PHP reads the text file content and displays it in pages
  • php auto Define a complete example of paging class
  • Two versatile PHP paging classes
  • 3 ways to implement paging function in PHP
  • Detailed explanation of PHP AJAX non-refresh paging implementation method

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1095683.htmlTechArticleOracle paging function example implemented by PHP, phporacle paging example This article describes the oracle paging function implemented by PHP. Share it with everyone for your reference, the details are as follows: mysql has limit, divided into...
Statement:
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