当前位置:中国站长下载文章中心数据库区ACCESS → 一个访问ACCESS的类

一个访问ACCESS的类

减小字体 增大字体 作者:佚名  来源:不详  发布时间:2006-5-9 16:05:00

这是access的类

<?
class accessdbm
{
var $count = 0;
var $values = array();
var $file = "";
var $error = "";
var $exists = false;
var $static = false;
var $exact = false;
var $dbm;

// older version of php can't do the 'new classname(args)'
// use initilize() if this is the case.

// *******************************************************

function accessdbm ($dbmfile, $static = 0)
{
global $php_errormsg;

if(!empty($dbmfile))
{
if(file_exists($dbmfile))
{
$this->exists = true;
}
if($static != 0)
{
$this->static = true;
}
$this->file = $dbmfile;
}
return;
}

// *******************************************************

// identical to accessdbm
function initialize ($dbmfile, $static = 0)
{
global $php_errormsg;

if(!empty($dbmfile))
{
if(file_exists($dbmfile))
{
$this->exists = true;
}
if($static != 0)
{
$this->static = true;
}
$this->file = $dbmfile;
}
return;
}

// *******************************************************

function add_entry ($key, $val)
{
$results = 0;
$dbm = $this->open_dbm();
if(!$dbm) { return false; }

if(!(dbmreplace($dbm,$key,$val)))
{
if(!(dbmexists($dbm,$key)))
{
$this->error = "fatal error : could not replace $key with $val";
$this->close_dbm($dbm);
return false;
}
}
$this->close_dbm($dbm);
return true;
}

// *******************************************************

function remove_entry ($key)
{
global $php_errormsg;
$removed = false;

$dbm = $this->open_dbm();
if(!$dbm) { return false; }

if(dbmexists($dbm,$key))
{
if(!dbmdelete($dbm,$key))
{
if(dbmexists($dbm,$key))
{
$this->error = "unable to remove [$key] : [$php_errormsg]";
$this->close_dbm($dbm);
return false;
}
}
else
{
$this->close_dbm($dbm);
$removed = true;
}
}
else
{
$this->error = "key [$key] does not exist";
$this->close_dbm($dbm);
return false;
}
return true;
}

// *******************************************************

function get_value ($key)
{
$val = "";
$readonly = true;

$dbm = $this->open_dbm($readonly);

if(!$dbm) { return false; }

if(dbmexists($dbm,$key))
{
$val = dbmfetch($dbm,$key);
}
$this->close_dbm($dbm);
return $val;
}

// *******************************************************

function open_dbm ($readonly = false)
{
global $php_errormsg;

if($this->static)
{
if(!(empty($this->dbm)))
{
$dbm = $this->dbm;
return ($dbm);
}
}

$filename = $this->file;

if(!$this->exists)
{
$dbm = @dbmopen($filename,"n");
}
else
{
if(!$readonly)
{
// we want the warning here if we can't be
// a writer
$dbm = dbmopen($filename,"w");
}
else
{
$dbm = @dbmopen($filename,"r");
}
}
if( (!$dbm) or (empty($dbm)) )
{
$this->exists = false;
$this->static = false;
$this->error = "unable to open [$filename] [$php_errormsg]";
return false;
}
$this->exists = true;
if($this->static)
{
$this->dbm = $dbm;
}

return ($dbm);

}

// *******************************************************

function find_key ($search)
{
$val = "";

$dbm = $this->open_dbm(1);
if(!$dbm) { return false; }
if(dbmexists($dbm,$search))
{
// wow an exact match
$val = dbmfetch($dbm,$search);
$this->close_dbm($dbm);
$this->exact = true;
return $val;
}
else
{
$this->exact = false;
$key = dbmfirstkey($dbm);
while ($key)
{
// strip the first whitespace char and
// everything after it.
$test = ereg_replace(" .*","",$key);
if(eregi("^$test",$search))
{
$val = dbmfetch($dbm,$key);
$this->close_dbm($dbm);
error_log("test [$test] matched [$search]",0);
return $val;
}
$key = dbmnextkey($dbm,$key);
}
}
// didn't find it
$this->close_dbm($dbm);
return false;
}

// *******************************************************

// returns the key
function find_val ($search)
{
$this->exact = false;

$dbase = $this->get_all();
if(empty($dbase))
{
error_log("error dbase is empty $db->error",0);
r

[1] [2]  下一页