这是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 cant 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 cant 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);
}
}
// didnt 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);
return false;
}
while ( list ( $key, $val ) = each ($dbase) )
{
if($search == $val)
{
$this->exact=true;
return $key;
}
else
{
// strip the first whitespace char and
// everything after it.
$test = ereg_replace(" .*","",$val);
if(eregi("^$test",$search))
{
$this->exact = false;
return $key;
}
}
}
// didnt find it
return false;
}
// *******************************************************
function get_all ()
{
$values = array();
$count = 0;
$readonly = true;
$dbm = $this->open_dbm($readonly);
if(!$dbm) { return false; }
$key = dbmfirstkey($dbm);
while ($key)
{
$val = dbmfetch($dbm,$key);
$values[$key] = $val;
$count++;
$key = dbmnextkey($dbm, $key);
}
$this->count = $count;
$this->values = $values;
$this->close_dbm($dbm);
return $values;
}
// *******************************************************
function close_dbm ($dbm)
{
$results = false;
if(!$this->static)
{
$results = dbmclose($dbm);
}
return $results;
}
// *******************************************************
function static_close()
{
$results = false;
if(!$this->dbm)
{
$this->error = "no static dbm to close";
return false;
}
$dbm = $this->dbm;
$results = dbmclose($dbm);
unset($this->dbm);
return $results;
}
// *******************************************************
}
?>
这个连接上!
include("class.accessdbm.php3");
$static = true;
$dbase = new accessdbm("/path/to/file.dbm",$static);
register_shutdown_function($dbase->static_close());
if(!$dbase->add_entry("cdi","cdi@thewebmasters.net))
{
echo "error adding entry: $dbase->error\n";
}
$values = $dbase->get_all()
while ( list ($key,$val) = each ($values) )
{
echo "key: $key val: $val \n";
}
exit;
