不是我想用ACCESS的,么办法啊,用户即上帝么。
更何况这上帝不懂数据库啥的……与其劝他换个服务商还不如委屈自己写个类库将就一下。
网上有ODBC版本,但在我这用有点小小的版本问题……又是让人无奈的上帝。
贴代码啦,代码晕眩症的就不用点全文链接了。
- <?php
- /*--------------------------------------------------------------------
- FileName:db.php
- Summary: PHP-Access Database Operation Class
- Author: Jerry Hong
- CreateTime: 2007-8-10
- LastModifed: 2007-8-25 : Added UTF-8 Support
- copyright (c)2007
- http://www.JerryHong.com
- [email]hjrgenius@163.com[/email]
- --------------------------------------------------------------------*/
- class ADODB
- {
- var $conn;
- var $rs; //RecordSet
- var $fld; //Field
- var $results; //Mixed array for query result.
- function ADODB($dbname, $dbuser='', $dbpassword='') {
- return $this->__construct($dbname, $dbuser, $dbpassword);
- }
- /**
- * Connects to a database
- * @param string $dbname
- * @param string $dbuser
- * @param string $dbpassword
- */
- function __construct($dbname, $dbuser, $dbpassword) {
- $this->conn = new COM("ADODB.Connection", NULL, 65001) or bail('Cannot create ado connection, please contact administrator'.$adminEmail);
- register_shutdown_function(array(&$this, "__destruct"));
- $connStr="DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=". realpath($dbname).
- ";UID=".$dbuser.
- ";PWD=".$dbpassword;
- //echo $connStr;
- $this->conn->Open($connStr);
- $this->conn->charPage ='65001';
- /*
- PHP5 can use these codes to replace the up line:
- try{
- $this->conn->Open($connStr) ;
- }
- catch(exception $e){
- $this->bail('Cannot open database, please contact administrator');
- }
- */
- return true;
- }
- //GOT BUG HERE!
- function __destruct() {
- if($this->rs)
- {
- //$this->rs->Close();
- //$this->rs->Release();
- }
- if($this->conn)
- {
- //$this->conn->Close();
- //$this->conn->Release();
- }
- unset($this);
- return true;
- }
- function __sleep(){
- }
- //Data Funcs//////////////////////////////////////////////
- /**
- * Execute a query
- * @param string $sql
- */
- function query($sql) {
- $this->rs = $this->conn->Execute($sql);
- }
- /**
- * Return an entire result set from the database
- * @param string $query (can also be null to pull from the cache)
- * @return mixed results
- */
- function get_results($sql) {
- $results=array();
- $this->query($sql);
- $num_columns = $this->get_col_num();
- for ($i=0; $i < $num_columns; $i++)
- {
- $this->fld[$i] =$this->rs->Fields($i);
- }
- $rowcount = 0;
- while (!$this->rs->EOF)
- {
- for ($i=0; $i < $num_columns; $i++)
- {
- $results[$rowcount][$this->fld[$i]->name] = $this->fld[$i]->value;
- }
- $rowcount++; // rowcount 自增
- $this->rs->MoveNext();
- }
- return $results;
- }
- /**
- * Return an row of result set from the database
- * @param string $query (can also be null to pull from the cache)
- * @return mixed results
- */
- function get_result($sql) {
- $result=array();
- $this->query($sql);
- $num_columns = $this->get_col_num();
- for ($i=0; $i < $num_columns; $i++)
- {
- $this->fld[$i] =$this->rs->Fields($i);
- }
- for ($i=0; $i < $num_columns; $i++)
- {
- $result[$this->fld[$i]->name] = $this->fld[$i]->value;
- }
- return $result;
- }
- /**
- * Get the number of columns of one table. Should run after "query('select * from table');" or it won't know the table name!
- * @return int column number
- */
- function get_col_num() {
- $col_num= $this->rs->fields->Count();
- return $col_num;
- }
- //Tool Funcs////////////////////////////////
- /**
- * Starts the timer, for debugging purposes
- */
- function timer_start() {
- $mtime = microtime();
- $mtime = explode(' ', $mtime);
- $this->time_start = $mtime[1] + $mtime[0];
- return true;
- }
- /**
- * Stops the debugging timer
- * @return int total time spent on the query, in milliseconds
- */
- function timer_stop() {
- $mtime = microtime();
- $mtime = explode(' ', $mtime);
- $time_end = $mtime[1] + $mtime[0];
- $time_total = $time_end - $this->time_start;
- return $time_total;
- }
- /**
- * Wraps fatal errors in a nice header and footer and dies.
- * @param string $message
- */
- function bail($message) { // Just wraps errors in a nice header and footer
- header('Content-Type: text/html; charset=utf-8');
- ?>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <title>database › Error</title>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- </head>
- <body>
- <p><?php echo $message; ?></p>
- </body>
- </html>
- <?php
- die();
- }
- }
- if ( ! isset($db) )
- $db = new ADODB(DB_FILE,DB_USER,DB_PASSWORD);
- ?>






谢谢!初学,正在找PHP和Access相关的资料,请问这个类该怎么使用呢?
@小伙伴(- -||)
1.在主程序中定义DB_FILE,DB_USER,DB_PASSWORD这三个值
2.在主程序中require这个类库
3.在主程序中使用$db->query($sql) 或 getresults($sql) 或 getresult($sql);(如果是在函数中别忘了先global $db;)
后两个有返回值,一个是二维数组,一个是一维数组,可以用print_r()看看。