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

帶數據緩存的ACCESS數據庫操作類-Access數據庫教程

編輯:關於Access數據庫

主要提供的功能:

1,基本的常用的就不說了

2,數據列表輸出,除了奇偶行交替變換顏色之類的列表,其他都可以完成,支持使用函數對字段的值進行格式化輸出。而且這個功能的效率應該還是能保證的,雖然像是一個模板解析函數了。首先,一行的Html代碼量並不多,而解析,也僅僅是運行了一個preg_replace和str_replace就完成了,而且解析一次,在列表輸出時,就不需要再次解析了,所以,此功能也僅僅就是比手動編寫的多了那兩個函數而已,幾乎可以忽略不計。

3,分頁查詢,可通過類提供的一個方法,簡單完成分頁的查詢。生成的SQL語句是我在網上找的,據說時不用存儲過程最高效率的了,而Access也不支持存儲過程

4,顯示分頁,提供了兩種樣式,一種適合分頁比較少的短樣式,一種是分頁比較多的長洋式,顯示效果與動易CMS的分頁相同,其實就是模仿動易的。

5,顯示分頁需要的相關數據,在分頁查詢函數中已經完成,所以,即使不使用類提供的分頁樣式,也可以方便的自己訂制

6,數據緩存,只需要配置好,緩存路徑,緩存生命周期,如果是分頁查詢的話,在設置一下最多緩存多少分頁,就不需要再作任何的干預,和不使用數據緩存的使用方法,接口完全相同。使用效果我沒有測試,不過應該還是可以保證的,數據緩存是數組形式的

代碼:

以下為引用的內容:

<?PHP
class Access
{
/**
* 聲明存儲查詢結果ID的數組,數據庫連接ID,存儲分頁信息的數組,緩存數據讀取偏移量
*/
public $resultId, $linkId, $pageMsg, $offset;
/**
* 聲明顯示錯誤消息的頁面地址
*/
public $errPage = '';
/**
* 聲明數據庫路徑,此路徑需為絕對路徑
*/
public $dbPath = '';

/**
* 緩存存儲路徑
*/
public $cachePath = '';

/**
* 緩存聲明周期,設為0則不適用緩存
*/
public $cacheLifeTime = 3600;

/**
* 當使用分頁查詢時,最多緩存多少頁
*/
public $cacheLimitMax = 100;

/**
* 建立數據庫連接
*
* 說明:
* 此數據庫類無構造函數,在聲明新類之後,需手動運行此函數
*/
public function connect()
{
$dsn = 'DRIVER={Microsoft Access Driver (*.mdb)}; DBQ='.$this->dbPath;
$this->linkId = odbc_connect($dsn,'','',SQL_CUR_USE_ODBC);
$this->linkId || $this->setError('Connect database defeat!');
}

/**
* 執行一條SQL語句
*
* 參數:
* $sql 要執行的SQL語句
* $resultId 查詢結果的ID,當執行一條不需返回的SQL語句,如刪除,更新等時,該參數可省略
*/
public function query($sql ,$resultId = '__id__')
{
$this->resultId[$resultId] = odbc_exec($this->linkId,$sql);
$this->resultId[$resultId] || $this->setError('CarrIEs out the SQL defeat!');
}

/**
* 從查詢結果集中讀取一條記錄,並返回為數組
*
* 參數:
* $resultId 查詢結果的ID
*/
public function record($resultId)
{
if (is_array($this->resultId[$resultId]))
{
$offset = $this->offset[$resultId]; $this->offset[$resultId] ;
return $this->resultId[$resultId][$offset];
}
return odbc_fetch_array($this->resultId[$resultId]);
}

/**
* 從查詢結果集中讀取一條記錄,並注冊為類的屬性,屬性名為字段名
*
* 參數:
* $resultId 查詢結果ID
*/
public function recordObj($resultId)
{
if (is_array($this->resultId[$resultId]))
{
$rowArray = $this->resultId[$resultId][$this->offset[$resultId]];
$this->offset[$resultId] ;
} else {
$rowArray = $this->record($resultId);
}
for (reset($rowArray);$key = key($rowArray);next($rowArray)) $this->$key = $rowArray[$key];
}

/**
* 獲取一個查詢結果集的記錄數
*
* 參數:
* $resultId 查詢結果ID
*/
public function rowsNum($resultId)
{
return odbc_num_rows($this->resultId[$resultId]);
}

/**
* 獲取表中符合條件的記錄總數
*
* 參數:
* $table 表明
* $primary 主鍵,提供一個主鍵時可提高性能
* $condition 查詢條件,留空時將返回表中的記錄總數
*/
public function rowsTotal($table, $primary = '*', $condition = '')
{
$sql = 'select ('.$primary.') from '.$table.($condition ? ' where '.$condition : '');
$rowsTotal = odbc_result(odbc_exec($this->linkId,$sql),1);
$rowsTotal >= 0 || $this->setError('Gains the record total defeat!');
return (int)$rowsTotal;
}

/**
* 釋放一個查詢結果
*
* 參數:
* $resultId 查詢結果ID
*/
public function resultFree($resultId)
{
odbc_free_result($this->resultId[$resultId]) || $this->setError('Release result defeat!');
}

/**
* 釋放所有查詢結果
*/
public function allResultFree()
{
for (reset($this->resultId);$key = key($this->resultId);next($this->resultId)) '__id__' == $key || $this->resultFree($key);
}

/**
* 釋放所有查詢結果並關閉數據庫連接
*/
public function close()
{
$this->allResultFree(); odbc_close($this->linkId);
}

/**
* 數據庫查詢
*
* 參數:
* $resultId 查詢結果ID
* $table 所要查詢的數據表
* $fIElds 需要返回的字段,省略時將返回所有字段
* $condition 查詢條件,省略時,將返回表中的所有記錄
*/
public function select($resultId, $table, $fIElds = '*', $condition = '')
{
if ($this->cacheLifeTime)
{
$cachePath = $this->cachePath.$table.md5($fIElds.$condition).'.PHP';
if (time() - @filemtime($cachePath) < $this->cacheLifeTime)
{
include $cachePath; $this->resultId[$resultId] = $dataCache;
$this->offset[$resultId] = 0; return;
} else {
$writeCache = true;
}
}
$condition && $condition = 'order ' == substr($condition,0,6) ? $condition : ' where '.$condition;
$this->query('select '.$fIElds.' from '.$table.$condition,$resultId);
$writeCache && $this->writeCache($cachePath,$resultId);
}

/**
* 插入記錄
*
* 參數:
* $table 表明
* $rowArray 二維數組,索引為字段名
*/
public function insert($table,$rowArray)
{
$fIElds = $values = '';
for (reset($rowArray); $key = key($rowArray);next($rowArray))
{
$fIElds .= ','.$key; $values .= ',\''.$rowArray[$key].'\'';
}
$this->query('insert into '.$table.'('.substr($fIElds,1).') values('.substr($values,1).')');
}

/**
* 更新一條記錄
*
* 參數:
* $table 表名
* $rowArray 二維數組,索引為字段名
* $condition 更新條件
*/
public function update($table,$rowArray,$condition)
{
$fIElds = '';
for (reset($rowArray);$key = key($rowArray);next($rowArray)) $fIElds .= ','.$key.'=\''.$rowArray[$key].'\'';
$this->query('update '.$table.' set '.substr($fIElds,1).' where '.$condition);
}

/**
* 刪除記錄
*
* 參數:
* $table 表明
* $condition 刪除條件,當省略時,刪除表中的所有記錄
*/
public function delete($table,$condition = '')
{
$this->query('delete from '.$table.($condition ? ' where '.$condition : ''));
}

/**
* 輸出數據列表
*
* 參數:
* $resultId 查詢結果ID
* $rowHtml 列表的行Html代碼
* $everyOther 每隔幾行插入$insertHtml
* $insertHtml 需要插入的Html
*
* $rowHtml的編寫規則:
* <td>{$name}</td><td>{date('Y-m-d',strtotime($addtime))}</td>
* 需要輸出的字段或用來格式化字段的函數需用{和}包括
* 變量名使用字段名
*/
public function displayList($resultId,$rowHtml,$everyOther = '',$insertHtml = '')
{
$rowHtml = preg_replace('/\$([A-Za-z0-9_] )/','$rowArray[\'\\1\']',$rowHtml);
$rowHtml = 'echo \''.str_replace(array('{','}'),array('\',',',\''),$rowHtml).'\'';
$i = 1;
while ($rowArray = $this->record($resultId))
{
eval($rowHtml);
if ($everyOther == $i) { echo $insertHtml; $i = 1; }
$i = $i 1;
}
}

/**
* 分頁查詢函數
*
* 參數:
* $resultId 查詢結果ID
* $table 所要查詢的數據表名
* $fIElds 需要返回的字段
* $primary 用來排序的字段
* $page 查詢第幾頁
* $pageSize 每頁記錄數
* $condition 查詢條件,默認為空
* $order 排序方式,0為正序,1為倒序,默認為1
*
* 說明:
* 此函數會將與分頁相關的信息存儲於$this->pageMsg[$resultId]中
* 這是一個一維數組,具有5個值,分別為:記錄總數,總頁數,當前頁記錄數,當前第幾頁,每頁多少條記錄
* 可利用這些信息編寫自己的分頁樣式,不需另外計算
*/
public function limit($resultId,$table,$fIElds,$primary,$page,$pageSize,$condition = '',$order = 1)
{
isset($this->pageMsg[$resultId][0]) || $this->pageMsg[$resultId][0] = $this->rowsTotal($table,$primary,$condition);
$this->pageMsg[$resultId][1] = ceil($this->pageMsg[$resultId][0]/$pageSize);
$page > $this->pageMsg[$resultId][1] && $page = $this->pageMsg[$resultId][1];
$this->pageMsg[$resultId][2] = $page == $this->pageMsg[$resultId][1] ? ($this->pageMsg[$resultId][0]-($page-1)*$pageSize) : $pageSize;
$this->pageMsg[$resultId][3] = $page;
$this->pageMsg[$resultId][4] = $pageSize;
if ($this->cacheLifeTime && $page <= $this->cacheLimitMax)
{
$cachePath = $this->cachePath.$table.'_'.$page.'.PHP';
if (time() - @filemtime($cachePath) < $this->cacheLifeTime)
{
include $cachePath; $this->resultId[$resultId] = $dataCache;
$this->offset[$resultId] = 0; return;
} else $writeCache = true;
}
if ($order)
{
$mark = '<'; $min = 'min'; $order = ' order by '.$primary.' desc';
} else {
$mark = '>'; $min = 'max'; $order = '';
}
$sql = 'select top '.$this->pageMsg[$resultId][2].' '.$fIElds.' from '.$table;
if (1 == $page)
{
$sql .= ($condition ? ' where '.$condition : '').$order;
} else {
$sql .= ' where '.$primary.$mark.'(select '.$min.'('.$primary.') from (select top '.($page-1)*$pageSize;
$sql .= ' '.$primary.' from '.$table.$order.')) '.($condition ? 'and '.$condition : '').$order;
}
$this->query($sql,$resultId);
$writeCache && $this->writeCache($cachePath,$resultId);
}

public function displayLimit($resultId,$linkHtml,$style = 2,$recordName = '條記錄')
{
if (2 == $style)
{
echo '共&nbsp;<strong>',$this->pageMsg[$resultId][0],'</strong>&nbsp;',$recordName,' ';
}
echo '<a href=',str_replace('*','1',$linkHtml),'>首頁</a>&nbsp;';
if (1 == $this->pageMsg[$resultId][3])
{
echo '上一頁&nbsp;';
} else {
echo '<a href=',strtr('*',$this->pageMsg[$resultId][3]-1,$linkHtml),'>上一頁</a>&nbsp;';
}
if ($this->pageMsg[$resultId][3] == $this->pageMsg[$resultId][1])
{
echo '下一頁';
} else {
echo '<a href=',strtr('*',$this->pageMsg[$resultId][3] 1,$linkHtml),'>下一頁</a>';
}
echo '&nbsp;<a href=',strtr('*',$this->pageMsg[$resultId][1],$linkHtml);
echo '>尾頁</a>&nbsp;頁次:<strong><font color=#ff0000>';
echo $this->pageMsg[$resultId][3],'</font>/',$this->pageMsg[$resultId][1],'</strong>頁';
if (2 == $style)
{
echo ' <strong>',$this->pageMsg[$resultId]['e'],'</strong>',$recordName,'/頁&nbsp;轉到';
echo ':<select name=page size=1 onchange="Javascript:window.location=';
echo 'this.options[this.selectedIndex].value;" style=font-size:12px;height=18px>';
for ($i=1;$i<=$this->pageMsg[$resultId][1];$i )
{
echo '<option value=\'',strtr('*',$i,$linkHtml);
echo $this->pageMsg[$resultId][3] == $i ? '\' selected ' : '\'','>第',$i,'頁</option>';
}
echo '</select>';
}
}

/**
* 將查詢結果輸入緩存
*
* 參數:
* $cachePath 緩存路徑
* $resultId 查詢結果ID
*/
private function writeCache($cachePath,$resultId)
{
$cacheContent = '';
while ($rowArray = odbc_fetch_array($this->resultId[$resultId]))
{
$cacheContent .= '$dataCache[]=array('.$this->rowToStr($rowArray).');';
}
file_put_contents($cachePath,'<?PHP '.$cacheContent.' ?>');
include $cachePath; $this->resultId[$resultId] = $dataCache;
$this->offset[$resultId] = 0;
}

/**
* 將數組轉換為一個二維數組結構的字符串
*
* 參數:
* $rowArray 數組
*/
private function rowToStr($rowArray)
{
for (reset($rowArray);$key = key($rowArray);next($rowArray))
{
$rowStr .= ',\''.$key.'\'=>\''.strtr($rowArray[$key],'\'','\\\'').'\'';
}
return substr($rowStr,1);
}

/**
* 調用錯誤消息頁面,完成錯誤消息的顯示
*
* 參數:
* $msg 錯誤消息
*/
public function setError($msg)
{
include $this->errPage;
}
}
?>

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved