|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
php manual(PHP手册)肯定是要从网上下载一个的,它很权威,也很全面,我自己认为它是一本很好的参考书,但是不适合新手当教材使用。数据|数据库|数据库毗连 原文申明:今朝此类只撑持Sybase,略加扩大就能够同时撑持其它多种数据库,功效还不完美,然而根基上够用
,别的,还没有写申明文档,先放下去,有需求的伴侣可以拿去看看,需求SQL Relay撑持哦。
【操作类代码】
// pdbconn.inc.php
<?php
/**
* 全局常量界说
*
*/
define("SYBASE", 0);
define("ORACLE", 1);
define("SUCCESS", 1);
define("FAILED", -1);
define("DEBUG", 1);
/**
* 数据库毗连类
* 入手下手于:2006年1月10日
* 作者 :秋衫客 (cntoby@gmail.com)
*
* 功效描写:数据库查询及毗连池保护
* 临时不撑持ORACLE
*/
class dbconn {
var $_conn; // 以后毗连ID
var $_cur; //
var $_dbhost = "localhost"; // 数据库办事器名
var $_dbport = 9000; // 数据库端标语
var $_dbuser = "dev"; // 登录数据库的用户名
var $_dbpass = "dev"; // 登录数据库的暗码
var $_dbsock = "null"; // 数据库毗连的Unix套接字
var $_dbflag = SYBASE; // 数据库类型
var $_dbflagS = "sybase"; // SQLRelay中数据库类型标记
var $_dbrtrt = 0; // 毗连掉败后直接多长工夫重试
var $_dbtries = 1; // 重试次数
var $_debug = false; // 是不是为DEBUG形式
var $_cursql = ""; // 以后存在的SQL文
var $_lastrt = true; // 最初一次履行的了局,胜利与否boolean
var $_queryrs = null; // 查询过程当中的了局
var $rs = null; // 最初一次履行了局
var $recordcount = 0; // 当次前往纪录总数
var $absolute = 0; // 以后页码
var $pagesize = 0; // 每页显示纪录数
var $pagecount = 1; // 本次查询总页数
var $total = 0; // 总纪录数
/**
* 机关函数,在本函数内初始化毗连及毗连池
* 机关函数的参数申明:
* 不必任何参数可以使用默许参数毗连数据库
* 当利用参数时,可使用1到5个参数,按次为
* dbhost 数据库办事器名
* dbuser 登录数据库用户名
* dbpass 登录数据库暗码
* dbsock 数据库毗连的Unix套接字
* dbport 数据库端标语
* dbflag 数据库类型,可选类型为SYBASE或ORACLE
*
* @return dbconn
*/
function dbconn() {
$argc = func_num_args();
$argv = func_get_args();
$this->_dbhost = (!empty($argv[0]))?$argv[0]:$this->_dbhost;
$this->_dbuser = (!empty($argv[1]))?$argv[1]:$this->_dbuser;
$this->_dbpass = (!empty($argv[2]))?$argv[2]:$this->_dbpass;
$this->_dbsock = (!empty($argv[3]))?$argv[3]:$this->_dbsock;
$this->_dbport = (!empty($argv[4]))?$argv[4]:$this->_dbport;
$this->_dbflag = (!empty($argv[5]))?$argv[5]:$this->_dbflag;
$this->_dbrtrt = (!empty($argv[6]))?$argv[6]:$this->_dbrtrt;
$this->_dbtries = (!empty($argv[7]))?$argv[7]:$this->_dbtries;
$this->_debug = defined("DEBUG");
switch ($this->_dbflag) {
case SYBASE:
$this->_conn = @sqlrcon_alloc($this->_dbhost, $this->_dbport, $this->_dbsock, $this->_dbuser, $this->_dbpass, $this->_dbrtrt, $this->_dbtries);
$this->_cur = @sqlrcur_alloc($this->_conn);
if(@sqlrcon_identify($this->_conn)!="sybase"||@sqlrcon_ping($this->_conn)!=1) {
return FAILED;
}
break;
case ORACLE:
$this->_conn = @sqlrcon_alloc($this->_dbhost, $this->_dbport, $this->_dbsock, $this->_dbuser, $this->_dbpass, $this->_dbrtrt, $this->_dbtries);
$this->_cur = @sqlrcur_alloc($this->_conn);
if(substring(@sqlrcon_identify($this->_conn), 0, 6)!="oracle"||@sqlrcon_ping($this->_conn)!=1) {
return FAILED;
}
break;
default:
return FAILED;
break;
}
}
/**
* 设置用于查询的SQL文
*
* @param string $sql
*/
function setsql($sql = "") {
$this->_cursql = $sql;
return SUCCESS;
}
/**
* 重置到初始形态
*
* @return bool
*/
function rset() {
$this->rs = null;
$this->pagecount = 1;
$this->recordcount = 0;
$this->_queryrs = null;
$this->_lastrt = true;
return SUCCESS;
}
/**
* 履行查询,前往查询是不是胜利,参数可觉得一条SQL文,或一个SQL文数组
*
* @param string $sql
* @return bool
*/
function doexec($sql = "") {
$tran = false;
$isa = false;
$this->rset();
if(""==$sql||empty($sql)) {
$sql = $this->_cursql;
}
if(!is_array($sql)) {
if(!eregi("^select", trim($sql))) {
$tran = true;
}
}else {
$isa = true;
for($i = 0; $i < count($sql); $i++) {
if (!eregi("^select", trim($sql[$i]))) {
$tran = true;
break;
}
}
}
if ($tran) {
$tmp = @sqlrcur_sendQuery($this->_cur, "Begin Transaction");
if($tmp!=1) {
$this->_lastrt = false;
return FAILED;
}
}
if(!$isa) {
$tmp = @sqlrcur_sendQuery($this->_cur, $sql);
}else {
for ($i = 0; $i < count($sql); $i++) {
$tmp = @sqlrcur_sendQuery($this->_cur, $sql[$i]);
if($tmp!=1) break;
}
}
$this->setsql();
if($tmp==1) {
if($tran) @sqlrcur_sendQuery($this->_cur, "Commit Transaction");
$this->_lastrt = true;
}else {
if($tran) @sqlrcur_sendQuery($this->_cur, "Rollback Transaction");
$this->_lastrt = false;
return FAILED;
}
return SUCCESS;
}
/**
* 纪录查询,只前往一笔记录
*
* @return bool
*/
function doquery_row() {
$this->rset();
if((""==trim($this->_cursql))||empty($this->_cursql)) {
return false;
}
if(SYBASE==$this->_dbflag) {
@sqlrcur_sendQuery($this->_cur, "set rowcount 1");
$tmp = @sqlrcur_sendQuery($this->_cur, $this->_cursql);
if($tmp==1) {
$this->recordcount = @sqlrcur_rowCount($this->_cur);
$this->total = $this->recordcount;
if ($this->recordcount>0) {
$this->rs = @sqlrcur_getRow($this->_cur, 0);
}
}else {
$this->recordcount = -1;
$this->total = $this->recordcount;
@sqlrcur_sendQuery($this->_cur, "set rowcount 0");
return FAILED;
}
@sqlrcur_sendQuery($this->_cur, "set rowcount 0");
}elseif(ORACLE==$this->_dbflag) {
// 临时不撑持ORACLE
$sfmt = @ociparse($this->_conn, $this->_cursql);
$ret = @ociexecute($sfmt, OCI_DEFAULT);
if($ret) {
$this->recordcount = @oci_num_rows($sfmt);
$this->total = $this->recordcount;
if($this->recordcount>0) {
@ocifetchinto($sfmt, &$this->rs);
}
}else {
$this->recordcount = -1;
$this->total = $this->recordcount;
return FAILED;
}
}
$this->setsql();
return SUCCESS;
}
/**
* 纪录查询,前往多笔记录,可经由过程设置$rowcount来掌握前往纪录的最大数
*
* @param int $rowcount
* @return bool
*/
function doquery_rows($rowcount = 0) {
if(!eregi("[0-9]+", $rowcount)) {
$rowcount = 0;
}
$this->rset();
if((""==trim($this->_cursql))||empty($this->_cursql)) {
return FAILED;
}
if(SYBASE==$this->_dbflag) {
@sqlrcur_sendQuery($this->_cur, "set rowcount " .strval($rowcount));
$tmp = @sqlrcur_sendQuery($this->_cur, $this->_cursql);
if($tmp==1) {
$this->recordcount = @sqlrcur_rowCount($this->_cur);
$this->total = $this->recordcount;
if($this->recordcount > 0) {
if(0>=$this->pagesize||null==$this->pagesize) {
$this->pagecount = 1;
$startidx = 0;
$endidx = $this->recordcount - 1;
}else {
if($this->absolute<1) $this->absolute=1;
$this->pagecount = ceil($this->recordcount/$this->pagesize);
if($this->absolute>$this->pagecount) $this->absolute = $this->pagecount;
$startidx = $this->pagesize * ($this->absolute-1);
$endidx = $this->pagesize * $this->absolute -1;
}
$rcidx = 0;
// 以下保留了局到$this->rc
for($i = $startidx; $i <= $endidx&&$i<$this->total; $i++) {
if($rowcount!=0&&$rowcount==$i) break;
$this->rs[$rcidx++] = @sqlrcur_getRow($this->_cur, $i);
}
$this->recordcount = $rcidx;
}
}else {
$this->recordcount = -1;
$this->total = $this->recordcount;
@sqlrcur_sendQuery($this->_cur, "set rowcount 0");
return FAILED;
}
@sqlrcur_sendQuery($this->_cur, "set rowcount 0");
}elseif(ORACLE==$this->_dbflag) {
// 临时不撑持ORACLE
// Oracle 处置局部
$sfmt = @ociparse($this->_conn, $this->_cursql);
$ret = @ociexecute($sfmt, OCI_DEFAULT);
if($ret) {
$this->recordcount = @oci_num_rows($sfmt);
$this->total = $this->recordcount;
if($this->recordcount > 0) {
if($this->pagesize<=0||null==$this->pagesize) {
$this->pagecount = 1;
$startidx = 0;
$endidx = $this->recordcount - 1;
}else {
if($this->absolute < 1) $this->absolute = 1;
$this->pagecount = ceil($this->recordcount/$this->pagesize);
if($this->absolute>$this->pagecount) $this->absolute = $this->pagecount;
$startidx = $this->pagesize * ($this->absolute - 1);
$endidx = $this->pagesize * $this->absolute - 1;
// aaaaaaaaaaaaaa
}
}
}else {
$this->recordcount = -1;
$this->total = $this->recordcount;
return FAILED;
}
return FAILED;
}
$this->setsql();
return SUCCESS;
}
/**
* 封闭数据毗连
*
* @return bool
*/
function dbclose() {
@sqlrcon_free($this->_conn);
@sqlrcur_free($this->_cur);
}
/**
* 设置或获得每页显示的纪录数
*
* @param int $pagesize
* @return int
*/
function pagesize($pagesize = 0) {
if(eregi("[1-9]+", $pagesize)&&$pagesize!=0) {
$this->pagesize = intval($pagesize);
return $this->pagesize;
}else {
return $this->pagesize;
}
}
/**
* 设置或获得以后要显示的页次
*
* @param int $absolute
* @return int
*/
function absolute($absolute = 1) {
if(eregi("[1-9]+", $absolute)&&$absolute>1) {
$this->absolute = intval($absolute);
return $this->absolute;
}else {
return $this->absolute;
}
}
}
?>
学习数据库了,MYSQL可算是PHP的黄金搭档了,不过,虽然话是这么说,你也可能恨不得把MYSQL给生吞活剥了,因为这一行一列的东东简直让自己头晕目眩。 |
|