php 访问access mdb文件的类

    技术2023-03-29  28

    用这个的目的,是避免安装access软件,一般的access数据操作这个类能够实现

    <?php /* * Filename.....: class_mdb.php * Class........: mdb * Aufgabe......: open *.mdb MSAccess files * Erstellt am..: Donnerstag, 17. Juni 2004, 23:32:07 * _ __ _ _ * ||| | |/ / (_) | Wirtschaftsinformatiker IHK * /. ./| ' / _ __ _| |_ ___ www.ingoknito.de * - ^ -| < | '_ /| | __/ _ / * / - /| . /| | | | | || (_) | Peter Klauer * ||| |_|/_/_| |_|_|/__/___/ * mailto.......: knito@knito.de ,chenxy.china@gmail.com * * Changes: * 2011-01-19: added property and functions of tables in .mdb file by chenxy * 2006-03-08: changed function execute() to allow INSERT, UPDATE and DELETE. * Inserted $RecordsAffected which seems to take random values. At least * not the count of the records affected. * Thanks to Pete from DigiOz Multimedia for this hint. * 2005-09-27: added new field $rowcount which may be written by function execute() * added new field $ok which is filled after mdb() or open() is called * changed function execute( ) to have a second (optional) parameter $getrowcount (default false) * changed function mdb() to call open() and return the success to $mdb->ok as boolean * changed function open() to return the success as boolean * Using this class gets even more simple: function mdb() now calls open() directly * 2004-07-21: added function fieldcount() */ define("adSchemaTables", 20); define("adSchemaColumns", 4); class mdb { var $RS = 0; var $ADODB = 0; var $RecordsAffected; var $strProvider = 'Provider=Microsoft.Jet.OLEDB.4.0'; var $strMode = 'Mode=ReadWrite'; var $strPSI = 'Persist Security Info=False'; var $strDataSource = ''; var $strConn = ''; var $strRealPath = ''; var $recordcount = 0; var $ok = false; var $tbles; var $cls; /** * Constructor needs path to .mdb file * * @param string $dsn = path to *.mdb file * @return boolean success */ function mdb( $dsn='Please enter DataSource!' ) { $result = false; $this->strRealPath = realpath( $dsn ); if( strlen( $this->strRealPath ) > 0 ) { $this->strDataSource = 'Data Source='.$this->strRealPath; } else { echo "<br>mdb::mdb() File not found $dsn<br>"; return false; } $this->RecordsAffected = new VARIANT(); $this->tbles = array(); $this->cls = array(); $result = $this->open(); return $result; } // eof constructor mdb() function open( ) { $result = false; if( strlen( $this->strRealPath ) > 0 ) { $this->strConn = $this->strProvider.';'. $this->strDataSource.';'. $this->strMode.';'. $this->strPSI; $this->ADODB = new COM( 'ADODB.Connection' ); if( $this->ADODB ) { echo $this->strConn."/r/n"; $this->ADODB->open( $this->strConn ); if(($this->gettables())&&$this->getcols()) $result = true; } else echo '<br>mdb::open() ERROR with ADODB.Connection<br>'.$this->strConn; } $this->ok = $result; return $result; } // eof open() function gettables() { unset($this->tbles); $rs = $this->ADODB->OpenSchema(adSchemaTables); if(!$rs->eof()){ while(!$rs->eof()){ if($rs->Fields["TABLE_TYPE"]->value=="TABLE"){ $this->tbles[] = $rs->Fields["TABLE_NAME"]->value; } $rs->MoveNext(); } //var_dump($this->tbles); return $this->tbles; }else{ return false; } } function getcols() { $count = count($this->tbles); for($i=0;$i<$count;$i++){ $rs = $this->ADODB->OpenSchema(adSchemaColumns); if(!$rs->eof()){ unset($this->cls[$tbname]); while(!$rs->eof()){ if(in_array($rs->Fields["TABLE_NAME"]->value,$this->tbles)){ $tbname = $rs->Fields["TABLE_NAME"]->value; $this->cls[$tbname][] = $rs->Fields["COLUMN_NAME"]->value; } $rs->MoveNext(); } }else{ return false; } } //var_dump($this->cls); return $this->cls; } /** * Execute SQL-Statement * @param string $strSQL = sql statement * @param boolean $getrecordcount = true when a record count is wanted */ function execute( $strSQL, $getrecordcount = false ) { $this->RS = $this->ADODB->execute( $strSQL, &$this->RecordsAffected ); if( $getrecordcount == true ) { $this->RS->MoveFirst(); $this->recordcount = 0; # brute force loop while( $this->RS->EOF == false ) { $this->recordcount++; $this->RS->MoveNext(); } $this->RS->MoveFirst(); } } // eof execute() function eof() { return $this->RS->EOF; } // eof eof() function movenext( ) { $this->RS->MoveNext(); } // eof movenext() function movefirst() { $this->RS->MoveFirst(); } // eof movefirst() function close() { @$this->RS->Close(); // Generates a warning when without "@" $this->RS=null; @$this->ADODB->Close(); $this->ADODB=null; } // eof close() function fieldvalue( $fieldname ) { return $this->RS->Fields[$fieldname]->value; } // eof fieldvalue() function fieldname( $fieldnumber ) { return $this->RS->Fields[$fieldnumber]->name; } // eof fieldname() function fieldcount( ) { return $this->RS->Fields->Count; } // eof fieldcount() } // eoc mdb ?> 

     

    例子:

    <?php include 'class_mdb.php'; $mdb = new mdb('city_ip.mdb'); // your own mdb filename required if($mdb->ok){ echo "ok/r/n"; }else{ echo "open mdb file failed/r/n"; exit; } echo "dump tables list /r/n"; var_dump($mdb->tbles); echo "dump table's columns list /r/n"; var_dump($mdb->cls); $sql = "select * from ".$mdb->tbles[0]; $mdb->execute($sql,true); // your own table in the mdb file echo "recordcount=".$mdb->recordcount."/r/n"; $fieldname = $mdb->fieldname(0); while( !$mdb->eof() ) { echo $mdb->fieldvalue($fieldname); // using your own fields name echo " = "; echo $mdb->fieldvalue(0); // using the fields fieldnumber echo "/r/n"; $mdb->movenext(); } # # Going back to the first recordset for the second example # $mdb->movefirst(); # # This works, too: Make each Field an object. The values change # when the data pointer advances with movenext(). # $url = $mdb->RS->Fields(1); $bez = $mdb->RS->Fields(2); while( !$mdb->eof() ) { # works! echo $bez->value; echo ","; echo $url->value; echo "/r/n"; $mdb->movenext(); } $mdb->close(); ?> 

    最新回复(0)