####----为大家写的class,希望对你们有点帮助,没有版权,欢迎讨论!----####

laosan 2001-12-10 10:13:09
<?
#-----------------------dbconfig.php---------------------
$SQLDB =1; //0-mysql 1-pgsql

$dbname ="bitdb"; //example
$dbhost ="localhost"; //example
$dbuser ="test"; //example
$dbpassword ="123456"; //example
#------------------------------------------------------------
?>
以下是类,不用修改!
<?
#-----------------------dbfunction.php---------------------
######################################################
## Usage Method DataType
## $db= new PHPDB()
## $db->Connect() int
## $db->Query($sql) int
## $db->RowNo() int
## $db->NumRows() int
## $db->First() bool
## $db->Next() bool
## $db->Previous() bool
## $db->Last() bool
## $db->Eof() bool
## $db->Bof() bool
## $db->FetchObject() object
## $db->ErrorMsg() string
## $db->Close() int
#####################################################

require("./../include/dbconfig.php");

class PHPDB{ ########----start class----##########

var $connect;
var $result;
var $rowptr;
var $rows;
var $eof;
var $bof;

#----------------------------------------------------------------------
function PHPDB(){

$this->connect = -1;
$this->result = null;
$this->rowptr = -1;
$this->rows = 0;
$this->eof =false;
$this->bof =false;
$this->Connect();

}//endfunction PHPDB()
#-------------------------------------------------------------------
function Connect(){

global $SQLDB,$dbname,$dbhost,$dbuser,$dbpassword;

if($SQLDB){
$connid=@pg_connect("dbname=$dbname user=$dbuser password=$dbpassword");
}else{
$connid=@mysql_connect("$dbhost","$dbuser","$dbpassword");
if($connid) mysql_select_db("$dbname");
}//endif($SQLDB)

if($connid){ $this->connect=$connid; return $this->connect; }
else { $this->connect= -1; return 0; }

}//endfunction Connect()
#-------------------------------------------------------------------
function Close(){ global $SQLDB;

if($this->connect!=-1){
if($SQLDB){ if($this->result != null) pg_freeresult($this->result);
$closed=@pg_close($this->connect); }
else { if($this->result!= null) mysql_free_result($this->result);
$closed=@mysql_close($this->connect); }
return $closed;
}//endif($this->connect!=-1)
else{ return 0;}

}//endfunction Close()
#--------------------------------------------------------------------
function Query($sql){ global $SQLDB;

if($this->connect != -1){

if($SQLDB){
$qres = pg_exec($this->connect,$sql);
$this->rows =@pg_numrows($qres);
}//endif($SQLDB)
else {
$qres = mysql_query($sql,$this->connect);
$this->rows =@mysql_num_rows($qres);
}//endelse

if($this->rows>0){
$this->result=$qres;
$this->eof=false;
$this->bof=false;
$this->First();
}//endif($this->rows>0)
else {
$this->result=null;
$this->eof=true;
$this->bof=true;
$this->rowptr= -1;
}//endelse

return $this->result;

}//endif($this->connect != -1)
else{ $this->result=null; return 0;}

}//endfunction Query($sql)
#--------------------------------------------------------------------
function NumRows(){ global $SQLDB;

if($this->result == null){return 0;}
else{
if($SQLDB) $this->rows = pg_numrows($this->result);
else $this->rows = mysql_num_rows($this->result);
return $this->rows;
}//endelse

}//endfunction NumRows()
#---------------------------------------------------------------------
function NumFields(){ global $SQLDB;

if($this->result == null){ return 0;}
else{
if($SQLDB) return pg_numfields($this->result);
else return mysql_num_fields($this->result);
}//endelse

}//endfunction NumFields()
#---------------------------------------------------------------------
function RowNo(){ return $this->rowptr; }
#---------------------------------------------------------------------
function First(){

if($this->rows){
$this->rowptr=0;
$this->bof=false;
$this->eof=false;
return true;
}//endif($this->rows)
else { return false;}

}//endfunction First()
#---------------------------------------------------------------------
function Last(){

if($this->rows){
$this->rowptr=$this->NumRows()-1;
$this->bof=false;
$this->eof=false;
return true;
}//endif($this->rows)
else {return false;}

}//endfunction Last()
#------------------------------------------------------------------------
function Next(){
if($this->rowptr < $this->NumRows()-1){
$this->rowptr+=1; return true; }
else { $this->eof=true;return false; }

}//endfunction Next()
#------------------------------------------------------------------------
function Previous(){
if($this->rowptr > 0) { $this->rowptr-=1; return true; }
else { $this->bof=false; return false; }

}//endfunction Previous()
#------------------------------------------------------------------------
function Eof(){ return $this->eof; }
#------------------------------------------------------------------------
function Bof(){ return $this->bof; }
#------------------------------------------------------------------------
function ErrorMsg(){ global $SQLDB;

if($this->connect != -1){
if($SQLDB){ return pg_errormessage($this->connect);}
else { return mysql_error($this->connect); }
}//endif($this->connect != -1)

}//endfunction ErrorMsg()
#------------------------------------------------------------------------
function FetchObject(){ global $SQLDB;

if ($this->result == null || $this->rowptr == -1){ return false;}
else{
if($SQLDB) { $fobject = pg_fetch_object($this->result,$this->rowptr); }
else { mysql_data_seek($this->result,$this->rowptr);
$fobject = mysql_fetch_object($this->result); }

return $fobject;
}//endelse

}//endfunction FetchObject()
#########################################################################
function NumAffected(){ global $SQLDB;

if ($this->result == null){ return 0;}
else {
if($SQLDB) return pg_cmdtuples($this->result);
else return mysql_affected_rows($this->result);
}

}//endfunction NumAffected()
#-----------------------------------------------------------------------
function LastOID(){ global $SQLDB;

if ($this->result == null || $this->connect == -1 ){ return null;}
else {
if($SQLDB) return pg_getlastoid ($this->result);
else return mysql_insert_id($this->connect);
}

}//endfunction LastOID()
#------------------------------------------------------------------------
function QuerySafe($string){
// replace \' with '
$string = str_replace("\'", "'", $string);

// replace line-break characters
$string = str_replace("\n", "", $string);
$string = str_replace("\r", "", $string);

return $string;

}//endfunction QuerySafe($string)
#------------------------------------------------------------------------
function SQLSafe($string){
//replace \' with \'\'
//use this function only for text fields that may contain "'"'s
$string = str_replace("\'", "\'\'", $string);

return $string;

}//endfunction SQLSafe($string)
#------------------------------------------------------------------------
?>
...全文
138 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
laosan 2002-03-03
  • 打赏
  • 举报
回复
中国B/S网也是我贴的呀,大哥。
lgcfm 2002-01-22
  • 打赏
  • 举报
回复
楼主是盗别人的代码啊,我在中国B/S网上看到这段代码
laosan 2001-12-14
  • 打赏
  • 举报
回复
很长一端时间,我希望大家都来提意见。
我们先从小问题谈起先!!!
sandj 2001-12-12
  • 打赏
  • 举报
回复
谁那里还有烂西红柿?
IPMAN还加什么啊!你的论坛更新了没有啊?
怎么在你的网站上就是没法找到啊?
登记了邮件地址一直没有发过来!
laosan 2001-12-12
  • 打赏
  • 举报
回复
提意见的人,那哪儿去了?说说啊,共同进步嘛!
laosan 2001-12-11
  • 打赏
  • 举报
回复
是啊,我现在就在用postgreSQL!!
怎么样?来点鸡蛋,共同提高PHP程序员的实力是我的愿望。
munn 2001-12-11
  • 打赏
  • 举报
回复
是你自己写的吗?佩服
「已注销」 2001-12-11
  • 打赏
  • 举报
回复
很强啊,呵呵~!你用过postgreSQL??
Sam_liao 2001-12-11
  • 打赏
  • 举报
回复
先谢了,我已复制了一份回去慢慢看。如果能多提供一些就好了。
ipman 2001-12-11
  • 打赏
  • 举报
回复
让我想想还有什么要加的……
cngift 2001-12-11
  • 打赏
  • 举报
回复
扔个鸽子蛋...

<?php
define(DB_HOST,"");
define(DB_NAME,"");
define(DB_USER,"");
define(DB_PASS,"");

class DB {

var $Host = DB_HOST;
var $Database = DB_NAME;
var $User = DB_USER;
var $Password = DB_PASS;
var $Link_ID = 0;
var $Query_ID = 0;

function Halt($msg){

echo $msg;
exit;

}

function Connect(){

if($this->Link_ID == 0){

$this->Link_ID = mysql_pconnect($this->Host,$this->User,$this->Password);
mysql_select_db($this->Database,$this->Link_ID);

}

}

function Close(){

if($this->Link_ID != 0){

mysql_close($this->Link_ID);

}

}

function Query($sql){

if($this->Link_ID == 0){

$this->Connect();

}

$this->Query_ID = mysql_query($sql,$this->Link_ID);
return $this->Query_ID;

}

function NextRecord(){

$this->Record = array();
$this->Record = mysql_fetch_array($this->Query_ID);
return $this->Record;

}

function NumRows(){

$NumRows = mysql_num_rows($this->Query_ID);
return $NumRows;

}




}

?>
laosan 2001-12-11
  • 打赏
  • 举报
回复
为什么没有人扔鸡蛋!
icb 2001-12-11
  • 打赏
  • 举报
回复
鸡蛋准备。。。。。放~~~~~~~~~~~~~~~~
epowerlab 2001-12-11
  • 打赏
  • 举报
回复
昨天收藏的
Nizvoo 2001-12-11
  • 打赏
  • 举报
回复
已经收藏。。。慢慢再看。

21,891

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧