PHP教程之PHP連接ACCESS數(shù)據(jù)庫的類_PHP教程
推薦:如何用PHP獲取歌曲時間PHP代碼 ?php file =realpath(1.mp3); //當(dāng)前目錄下1.mp3 if (!file_exists(file)) { exit('文件不存在'); } player = new COM(WMPlayer.OCX); media = player-newMedia(file); time=media-duration;//總的秒數(shù) h=floor(time /3600);//小時 m=floor((tim
試過PHP連接ACCESS數(shù)據(jù)庫沒?這個用途不大,但有時候也用得著!我以前試圖將一個ASP程序改寫成PHP的,原程序的數(shù)據(jù)庫設(shè)計得比較完善,我打算開發(fā)時就用它的ACCESS數(shù)據(jù)庫 ,等程序完成后再轉(zhuǎn)換。這個程序改寫計劃最終擱淺了,但此過程中為了方便連接ACCESS數(shù)據(jù)庫,我寫了個PHP連接ACCESS數(shù)據(jù)庫的類,比較方便,希望有人用得著!
*/
class access{
function access(dir){ //構(gòu)析函數(shù),調(diào)用.com建立數(shù)據(jù)源,鏈接數(shù)據(jù)庫;參數(shù)為數(shù)據(jù)庫文件路徑
this->db=new com("adodb.connection");
this->db->open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=".realpath(dir));
}
function sql(sql){//參數(shù)為SQL查詢語句
if(preg_match("/limit +([0-9]*),([0-9]*)/",sql,out)){//正則分析sql語句,提取limit參數(shù),模擬MYSQL的分頁
limit1=out[1];
limit2=out[2];
sql=preg_replace("/limit +([0-9]*),([0-9]*)/","",sql);
}
rs=this->db->execute(sql);
if(strpos(sql,"select")===false or strpos(sql,"select")>5){//非查詢語句直接返回空值,防止錯誤提示出現(xiàn)
return "";
}
if(limit1==""){
limit1=0;
}
if(limit2==""){//如果沒有l(wèi)imit參數(shù),設(shè)limit2為總記錄數(shù)
limit2=1000000;
}
rs->move(limit1);//記錄指針移動到第limit1條記錄處
i=0;
while(!rs->eof and i<limit2){
foreach(rs->fields as rss){
rs_r[rss->name]=rss->value;
}
rs_rr[]=rs_r;
rs->move(1);
i++;
}
return rs_rr;
}
function getlist(db,p=""){//此函數(shù)用于將關(guān)聯(lián)數(shù)組各元素處理成相關(guān)變量,第二個參數(shù)為變量名前綴,防止重名
foreach(db as key=>dba){
global {p.key};
{p.key}=dba;
}
}
function yasuo(dir){//壓縮數(shù)據(jù)庫函數(shù),本函數(shù)用access::yasuo(dir);調(diào)用,dir為數(shù)據(jù)庫文件路徑,調(diào)用次函數(shù)之前必須將所有當(dāng)前數(shù)據(jù)庫的連接中斷!
ya =new com("JRO.JetEngine");
truedir=realpath(dir);
dirname=dirname(truedir);
prov="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=";
ya->CompactDatabase(prov.truedir,prov.dirname."/aatmp.mdb");//數(shù)據(jù)庫所在目錄必須有讀寫權(quán)限,否則會導(dǎo)致失敗!
unlink(truedir);
rename(dirname."/aatmp.mdb",truedir);
}
function add(file){//新建一個ACCESS數(shù)據(jù)庫 參數(shù)file為文件路徑,必須為相對路徑
dir=dirname(_SERVER["SCRIPT_FILENAME"])."/".file;
db=new com("ADOX.Catalog");
db->Create("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=dir");
}
}
?>
不知道怎么用吧!下面有示例!
include("access.php");
access::add("my.mdb");//創(chuàng)建數(shù)據(jù)庫
db=new access("my.mdb");//連接數(shù)據(jù)庫
sql=<<<eot
CREATE TABLE a (
id AUTOINCREMENT(1,1),
name varchar(10))
eot;
db->sql(sql);//建表
sql=<<<eot
insert into `a` (`name`) values('wo ri');
eot;
db->sql(sql);//插入數(shù)據(jù)
db->sql("insert into `a` (`name`) values('靠');");
sql="select * from a limit 1,1";
rs=db->sql(sql);//查詢
print_r(rs);
?>
分享:PHP教程之一個頁面執(zhí)行時間類PHP代碼 ?php class Timer//頁面執(zhí)行時間類 { var starttime;//頁面開始執(zhí)行時間 var stoptime;//頁面結(jié)束執(zhí)行時間 var spendtime;//頁面執(zhí)行花費時間 function getmicrotime()//獲取返回當(dāng)前微秒數(shù)的浮點數(shù) { list(usec,sec)=explode( ,microtime()); ret
- PHPNOW安裝Memcached擴(kuò)展方法詳解
- php記錄頁面代碼執(zhí)行時間
- PHP中獎概率的抽獎算法程序代碼
- apache設(shè)置靜態(tài)文件緩存方法介紹
- php對圖像的各種處理函數(shù)代碼小結(jié)
- PHP 關(guān)于訪問控制的和運(yùn)算符優(yōu)先級介紹
- 關(guān)于PHP語言構(gòu)造器介紹
- php/js獲取客戶端mac地址的實現(xiàn)代碼
- php5.5新數(shù)組函數(shù)array_column使用
- PHP preg_match的匹配多國語言的技巧
- php 中序列化和json使用介紹
- php采集文章中的圖片獲取替換到本地
- 相關(guān)鏈接:
- 教程說明:
PHP教程-PHP教程之PHP連接ACCESS數(shù)據(jù)庫的類
。