PHP读取Access数据库操作类
[08-23 22:09:55] 来源:http://www.duoxue8.com PHP教程 阅读:661次
PHP读取Access数据库操作类,标签:PHP技巧,php培训,php学习,php安装,http://www.duoxue8.com
我们都知道PHP和MySQL是绝配,但有时候根据项目要求需要使用PHP读取Access数据库。其实PHP可以读取目前主流的所有数据库,因此读取Access数据库也不是很难。我整理了一个PHP读取Access数据库的类,提供给大家。
php 代码复制内容到剪贴板
PHP读取Access数据库操作类 结束。
- <?php
- //Summary: Access数据库操作类
- //使用范例:
- //$database ="**.mdb";
- //$dbuser ="**";
- //$dbpwd ="**";
- //$tablepre ="discuz_";
- //$access = new Access($database,$dbuser,$dbpwd,$tablepre);
- //$query = "SELECT * FROM ----"
- //$access -> set_query($query);
- //$access -> query();
- class Access{
- var $databasepath;
- var $constr;
- var $dbusername;
- var $dbpassword;
- var $linkid;
- var $result;
- var $queryString;
- var $tablepre; //数据表前缀
- function __construct($database,$dbuser,$dbpwd,$pre,$mode){
- $this->databasepath=$database;
- $this->username=$dbuser;
- $this->password=$dbpwd;
- $this->tablepre=$pre;
- $this->connect($mode);
- }
- function Access($database,$dbuser,$dbpwd,$pre,$mode){
- $this->__construct($database,$dbuser,$dbpwd,$pre,$mode);
- }
- function connect($mode=false){
- $this->constr = "DRIVER={Microsoft Access Driver (*.mdb)};DBQ=" . realpath($this->databasepath);
- if($mode){
- $this->linkid = odbc_pconnect($this->constr,$this->username,$this->password,SQL_CUR_USE_ODBC);
- } else {
- $this->linkid = odbc_connect($this->constr,$this->username, $this->password,SQL_CUR_USE_ODBC);
- }
- return $this->linkid;
- }
- function set_query($sql) {
- $prefix="#@__";
- $sql = trim($sql);
- $inQuote = false;
- $escaped = false;
- $quoteChar = '';
- $n = strlen($sql);
- $np = strlen($prefix);
- $restr = '';
- for($j=0; $j < $n; $j++) {
- $c = $sql{$j};
- $test = substr($sql, $j, $np);
- if(!$inQuote) {
- if ($c == '"' || $c == "'") {
- $inQuote = true;
- $escaped = false;
- $quoteChar = $c;
- }
- } else {
- if ($c == $quoteChar && !$escaped) {
- $inQuote = false;
- } else if ($c == "\" && !$escaped) {
- $escaped = true;
- } else {
- $escaped = false;
- }
- }
- if ($test == $prefix && !$inQuote) {
- $restr .= $this->tablepre;
- $j += $np-1;
- } else {
- $restr .= $c;
- }
- }
- $this->queryString = $restr;
- }
- function query($id="me"){
- $this->result[$id] = @odbc_exec($this->linkid,$this->queryString);
- if(!$this->result[$id]) {
- $this->display_error("Execute Query False! <font color='red'>".$this->queryString."</font>");
- }
- }
- function first_array($id="me"){
- return odbc_fetch_array($this->result[$id]);
- }
- function fetch_row($id="me"){
- return odbc_fetch_row($this->result[$id]);
- }
- function total_num($id="me"){//取得记录总数
- return odbc_num_rows($this->result[$id]);
- }
- function close(){//关闭数据库连接函数
- @odbc_close($this->linkid);
- $this->free_all_result();
- }
- function free_result($id="me") {
- @mysql_free_result($this->result[$id]);
- }
- function free_all_result() {
- if(!is_array($this->result)) {
- return "";
- }
- foreach($this->result as $kk => $vv){
- if($vv) @odbc_free_result($vv);
- }
- }
- function insert($table,$field,$value){//插入记录函数
- $sql="INSERT INTO {$table} ({$field}) VALUES ({$value})";
- $this->set_query($sql);
- $this->query();
- }
- function getinfo($table,$requirement,$limit){//取得当条记录详细信息
- $sql="SELECT * FROM {$table} WHERE {$requirement}";
- $this->set_query($sql);
- $this->query();
- if($this->fetch_row()){
- for ($i=1;$i<$limit;$i++){
- $info[$i]=odbc_result($this->result["me"],$i);
- }
- }
- return $info;
- }
- function getlist($table,$field,$limit,$condition,$sort="ORDER BY id DESC"){//取得记录列表
- $sql="SELECT * FROM ".$table." ".$condition." ".$sort;
- $this->set_query($sql);
- $this->query();
- while ($this->fetch_row()){
- $id=odbc_result($this->result["me"],1);
- $requirement="{$field}={$id}";
- $recordlist[]=getinfo($table,$requirement,$limit);
- }
- return $recordlist;
- }
- function getfieldlist($table,$field,$fieldnum,$condition="",$sort=""){//取得记录列表
- $sql="SELECT ".$field." FROM ".$table." ".$condition." ".$sort;
- $this->set_query($sql);
- $this->query();
- while ($this->fetch_row()){
- for ($j=0;$j<$fieldnum;$j++){
- $info[$j]=odbc_result($this->result["me"],$j+1);
- }
- $rdlist[]=$info;
- }
- return $rdlist;
- }
- function updateinfo($table,$requirement,$set){//更新记录
- $sql="UPDATE {$table} SET {$set} WHERE {$requirement}";
- $this->set_query($sql);
- $this->query();
- }
- function deleteinfo($table,$requirement){//删除记录
- $sql="DELETE FROM {$table} WHERE {$requirement}";
- $this->set_query($sql);
- $this->query();
- }
- function deleterecord($table,$condition){//删除指定条件的记录
- $sql="DELETE FROM ".$table." WHERE ".$condition;
- $this->set_query($sql);
- $this->query();
- }
- function getcondrecord($table,$condition=""){// 取得指定条件的记录数
- $sql="SELECT COUNT(*) AS num FROM ".$table." ".$condition;
- $this->set_query($sql);
- $this->query();
- $num=odbc_num_rows($this->result["me"]);
- return $num;
- }
- function display_error($msg) {
- echo "<html>rn";
- echo "<head>rn";
- echo "<meta http-equiv='Content-Type' content='text/html; charset=gb2312'>rn";
- echo "<title>DedeCms Error Track</title>rn";
- echo "</head>rn";
- echo "<body>rn<p style='line-helght:150%;font-size:10pt'>rn";
- echo $msg;
- echo "<br/><br/>";
- echo "</p>rn</body>rn";
- echo "</html>";
- }
- }
- ?>
本文地址:http://www.frostsky.com/2011/04/php-access/
PHP读取Access数据库操作类 结束。
Tag:PHP教程,PHP技巧,php培训,php学习,php安装,电脑学习 - 编程入门 - PHP教程
PHP读取Access数据库操作类相关文章
- ·上一个:PHP防止SQL注入式攻击
- PHP读取Access数据库操作类
- › PHP读取Access数据库操作类
- › php读取xml中某个元素的内容(PHP5以上才支持)
- 在百度中搜索相关文章:PHP读取Access数据库操作类
- 在谷歌中搜索相关文章:PHP读取Access数据库操作类
- 在soso中搜索相关文章:PHP读取Access数据库操作类
- 在搜狗中搜索相关文章:PHP读取Access数据库操作类