帶數(shù)據(jù)緩存的ACCESS數(shù)據(jù)庫操作類(2)_Access數(shù)據(jù)庫教程
推薦:Access 2007數(shù)據(jù)庫添加附件Access允許您在數(shù)據(jù)庫表中包含附件。通過利用微軟的對(duì)象鏈接和嵌入(OLE)技術(shù),您可以將照片、圖表、文檔及其他文件存儲(chǔ)在您的Access數(shù)據(jù)庫中。數(shù)據(jù)庫附件是Access 2007版本的一個(gè)新特
代碼:
以下為引用的內(nèi)容:
<?php
class access
{
/**
* 聲明存儲(chǔ)查詢結(jié)果ID的數(shù)組,數(shù)據(jù)庫連接ID,存儲(chǔ)分頁信息的數(shù)組,緩存數(shù)據(jù)讀取偏移量
*/
public $resultId, $linkId, $pageMsg, $offset;
/**
* 聲明顯示錯(cuò)誤消息的頁面地址
*/
public $errPage = '';
/**
* 聲明數(shù)據(jù)庫路徑,此路徑需為絕對(duì)路徑
*/
public $dbPath = '';
/**
* 緩存存儲(chǔ)路徑
*/
public $cachePath = '';
/**
* 緩存聲明周期,設(shè)為0則不適用緩存
*/
public $cacheLifeTime = 3600;
/**
* 當(dāng)使用分頁查詢時(shí),最多緩存多少頁
*/
public $cacheLimitMax = 100;
/**
* 建立數(shù)據(jù)庫連接
*
* 說明:
* 此數(shù)據(jù)庫類無構(gòu)造函數(shù),在聲明新類之后,需手動(dòng)運(yùn)行此函數(shù)
*/
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!');
}
/**
* 執(zhí)行一條SQL語句
*
* 參數(shù):
* $sql 要執(zhí)行的SQL語句
* $resultId 查詢結(jié)果的ID,當(dāng)執(zhí)行一條不需返回的SQL語句,如刪除,更新等時(shí),該參數(shù)可省略
*/
public function query($sql ,$resultId = '__id__')
{
$this->resultId[$resultId] = odbc_exec($this->linkId,$sql);
$this->resultId[$resultId] || $this->setError('Carries out the SQL defeat!');
}
/**
* 從查詢結(jié)果集中讀取一條記錄,并返回為數(shù)組
*
* 參數(shù):
* $resultId 查詢結(jié)果的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]);
}
/**
* 從查詢結(jié)果集中讀取一條記錄,并注冊(cè)為類的屬性,屬性名為字段名
*
* 參數(shù):
* $resultId 查詢結(jié)果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];
}
/**
* 獲取一個(gè)查詢結(jié)果集的記錄數(shù)
*
* 參數(shù):
* $resultId 查詢結(jié)果ID
*/
public function rowsNum($resultId)
{
return odbc_num_rows($this->resultId[$resultId]);
}
/**
* 獲取表中符合條件的記錄總數(shù)
*
* 參數(shù):
* $table 表明
* $primary 主鍵,提供一個(gè)主鍵時(shí)可提高性能
* $condition 查詢條件,留空時(shí)將返回表中的記錄總數(shù)
*/
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;
}
/**
* 釋放一個(gè)查詢結(jié)果
*
* 參數(shù):
* $resultId 查詢結(jié)果ID
*/
public function resultFree($resultId)
{
odbc_free_result($this->resultId[$resultId]) || $this->setError('Release result defeat!');
}
/**
* 釋放所有查詢結(jié)果
*/
public function allResultFree()
{
for (reset($this->resultId);$key = key($this->resultId);next($this->resultId)) '__id__' == $key || $this->resultFree($key);
}
/**
* 釋放所有查詢結(jié)果并關(guān)閉數(shù)據(jù)庫連接
*/
public function close()
{
$this->allResultFree(); odbc_close($this->linkId);
}
/**
* 數(shù)據(jù)庫查詢
*
* 參數(shù):
* $resultId 查詢結(jié)果ID
* $table 所要查詢的數(shù)據(jù)表
* $fields 需要返回的字段,省略時(shí)將返回所有字段
* $condition 查詢條件,省略時(shí),將返回表中的所有記錄
*/
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);
}
/**
* 插入記錄
*
* 參數(shù):
* $table 表明
* $rowArray 二維數(shù)組,索引為字段名
*/
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).')');
}
/**
* 更新一條記錄
*
* 參數(shù):
* $table 表名
* $rowArray 二維數(shù)組,索引為字段名
* $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);
}
/**
* 刪除記錄
*
* 參數(shù):
* $table 表明
* $condition 刪除條件,當(dāng)省略時(shí),刪除表中的所有記錄
*/
public function delete($table,$condition = '')
{
$this->query('delete from '.$table.($condition ? ' where '.$condition : ''));
}
/**
* 輸出數(shù)據(jù)列表
*
* 參數(shù):
* $resultId 查詢結(jié)果ID
* $rowHtml 列表的行HTML代碼
* $everyOther 每隔幾行插入$insertHtml
* $insertHtml 需要插入的HTML
*
* $rowHtml的編寫規(guī)則:
* <td>{$name}</td><td>{date('Y-m-d',strtotime($addtime))}</td>
* 需要輸出的字段或用來格式化字段的函數(shù)需用{和}包括
* 變量名使用字段名
*/
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;
}
}
/**
* 分頁查詢函數(shù)
*
* 參數(shù):
* $resultId 查詢結(jié)果ID
* $table 所要查詢的數(shù)據(jù)表名
* $fields 需要返回的字段
* $primary 用來排序的字段
* $page 查詢第幾頁
* $pageSize 每頁記錄數(shù)
* $condition 查詢條件,默認(rèn)為空
* $order 排序方式,0為正序,1為倒序,默認(rèn)為1
*
* 說明:
* 此函數(shù)會(huì)將與分頁相關(guān)的信息存儲(chǔ)于$this->pageMsg[$resultId]中
* 這是一個(gè)一維數(shù)組,具有5個(gè)值,分別為:記錄總數(shù),總頁數(shù),當(dāng)前頁記錄數(shù),當(dāng)前第幾頁,每頁多少條記錄
* 可利用這些信息編寫自己的分頁樣式,不需另外計(jì)算
*/
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 '共 <strong>',$this->pageMsg[$resultId][0],'</strong> ',$recordName,' ';
}
echo '<a href=',str_replace('*','1',$linkHtml),'>首頁</a> ';
if (1 == $this->pageMsg[$resultId][3])
{
echo '上一頁 ';
} else {
echo '<a href=',strtr('*',$this->pageMsg[$resultId][3]-1,$linkHtml),'>上一頁</a> ';
}
if ($this->pageMsg[$resultId][3] == $this->pageMsg[$resultId][1])
{
echo '下一頁';
} else {
echo '<a href=',strtr('*',$this->pageMsg[$resultId][3] 1,$linkHtml),'>下一頁</a>';
}
echo ' <a href=',strtr('*',$this->pageMsg[$resultId][1],$linkHtml);
echo '>尾頁</a> 頁次:<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,'/頁 轉(zhuǎn)到';
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>';
}
}
/**
* 將查詢結(jié)果輸入緩存
*
* 參數(shù):
* $cachePath 緩存路徑
* $resultId 查詢結(jié)果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;
}
/**
* 將數(shù)組轉(zhuǎn)換為一個(gè)二維數(shù)組結(jié)構(gòu)的字符串
*
* 參數(shù):
* $rowArray 數(shù)組
*/
private function rowToStr($rowArray)
{
for (reset($rowArray);$key = key($rowArray);next($rowArray))
{
$rowStr .= ',\''.$key.'\'=>\''.strtr($rowArray[$key],'\'','\\\'').'\'';
}
return substr($rowStr,1);
}
/**
* 調(diào)用錯(cuò)誤消息頁面,完成錯(cuò)誤消息的顯示
*
* 參數(shù):
* $msg 錯(cuò)誤消息
*/
public function setError($msg)
{
include $this->errPage;
}
}
?>
分享:淺談Access數(shù)據(jù)庫用另一種方式管理密碼大家都知道,數(shù)據(jù)庫的安全性是很重要的,它直接影響到數(shù)據(jù)庫的廣泛應(yīng)用。用戶可以采用任意一種方法來保護(hù)數(shù)據(jù)庫應(yīng)用程序,也可以將幾種方法結(jié)合起來使用。利用Access數(shù)據(jù)庫自身提供的
- Access數(shù)據(jù)庫安全策略之ASP式
- 第N次被ACCESS的關(guān)鍵字涮
- Access中用Jet SQL語句刪除表關(guān)系
- Access報(bào)表打印如何自動(dòng)分頁
- Access完成累計(jì)余額的計(jì)算
- 搭建Access為主的Mdb數(shù)據(jù)庫
- 一句sql更新兩個(gè)表并可更新對(duì)應(yīng)的字段值具體實(shí)現(xiàn)
- MySQL查詢優(yōu)化:連接查詢排序limit(join、order by、limit語句)介紹
- 內(nèi)網(wǎng)ssh/mysql登錄緩慢的解決方法
- 使用準(zhǔn)則進(jìn)行條件查詢--1.4.從窗體中選擇查詢的條件
- 中文Access2000速成教程--1.1 使用“向?qū)А痹O(shè)計(jì)數(shù)據(jù)庫
- 中文Access2000速成教程--1.3 在“設(shè)計(jì)”視圖中設(shè)計(jì)表
Access數(shù)據(jù)庫教程Rss訂閱編程教程搜索
Access數(shù)據(jù)庫教程推薦
- 如何在ACCESS中壓縮當(dāng)前數(shù)據(jù)庫
- 為Access數(shù)據(jù)庫文件
- 解讀SQL注入漏洞初級(jí)應(yīng)用之Access篇
- 內(nèi)網(wǎng)ssh/mysql登錄緩慢的解決方法
- 用Access設(shè)計(jì)客觀試卷(3)
- Excel和Access間復(fù)制、和導(dǎo)出數(shù)據(jù)
- 如何判斷一個(gè)字段是否在表中
- 用Access創(chuàng)建簡(jiǎn)單MIS管理系統(tǒng)
- 談ACCESS轉(zhuǎn)化為SQL SERVER的注意事項(xiàng)
- 統(tǒng)計(jì)某個(gè)日期區(qū)間內(nèi)星期天的個(gè)數(shù)
猜你也喜歡看這些
- Access數(shù)據(jù)庫提示OleDbException (0x80004005): 操作必須使用一個(gè)可更新的查詢
- 使ACCESS數(shù)據(jù)庫保持同步
- 純編碼實(shí)現(xiàn)Access數(shù)據(jù)庫的建立或壓縮
- 如何防止ACCESS數(shù)據(jù)庫被下載
- 將Access數(shù)據(jù)庫移植到SQLServer
- 解答Perl下應(yīng)當(dāng)如何連接Access數(shù)據(jù)庫
- 數(shù)據(jù)庫并發(fā)問題詳述
- 數(shù)據(jù)在Access與Office組件間自由流動(dòng)
- 解讀access中數(shù)據(jù)表的自動(dòng)重新聯(lián)接
- 如何在退出整個(gè)系統(tǒng)前提示用戶
- 相關(guān)鏈接:
- 教程說明:
Access數(shù)據(jù)庫教程-帶數(shù)據(jù)緩存的ACCESS數(shù)據(jù)庫操作類(2)
。