程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> Access數據庫 >> 關於Access數據庫 >> 一個訪問ACCESS的類

一個訪問ACCESS的類

編輯:關於Access數據庫
這是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
eturn 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;
}
}
}
// didn't 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","[email protected]))
{
echo "error adding entry: $DBase->error\n";
}
$values = $DBase->get_all()
while ( list ($key,$val) = each ($values) )
{
echo "key: $key val: $val \n";
}
exit;
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved