Copy codeThe Code is as follows:
<? Php
--------------------------------------------------------------------
// FileName: class. php
// Summary: Access database operation class
// Author: forest
// CreateTime: 2006-8-10
// LastModifed:
// Copyright (c) 2006 freeweb.nyist.net /~ Chairy [email] chaizuxue@163.com [/email]
// Example:
// $ Databasepath = "database. mdb ";
// $ Dbusername = "";
// $ Dbpassword = "";
// Include_once ("class. php ");
// $ Access = new Access ($ databasepath, $ dbusername, $ dbpassword );
--------------------------------------------------------------------
Class Access
{
Var $ databasepath, $ constr, $ dbusername, $ dbpassword, $ link;
Function Access ($ databasepath, $ dbusername, $ dbpassword)
{
$ This-> databasepath = $ databasepath;
$ This-> username = $ dbusername;
$ This-> password = $ dbpassword;
$ This-> connect ();
}
Function connect ()
{
$ This-> constr = "DRIVER = {Microsoft Access Driver (*. mdb)}; DBQ =". realpath ($ this-> databasepath );
$ This-> link = odbc_connect ($ this-> constr, $ this-> username, $ this-> password, SQL _CUR_USE_ODBC );
Return $ this-> link;
// If ($ this-> link) echo "congratulations, the database connection is successful! ";
// Else echo "database connection failed! ";
}
Function query ($ SQL)
{
Return @ odbc_exec ($ this-> link, $ SQL );
}
Function first_array ($ SQL)
{
Return odbc_fetch_array ($ this-> query ($ SQL ));
}
Function fetch_row ($ query)
{
Return odbc_fetch_row ($ query );
}
Function total_num ($ SQL) // retrieve the total number of records
{
Return odbc_num_rows ($ this-> query ($ SQL ));
}
Function close () // closes the database connection function
{
Odbc_close ($ this-> link );
}
Function insert ($ table, $ field) // insert record function
{
$ Temp = explode (',', $ field );
$ Ins = '';
For ($ I = 0; $ I <count ($ temp); $ I ++)
{
$ Ins. = "'". $ _ POST [$ temp [$ I]. "',";
}
$ Ins = substr ($ ins, 0,-1 );
$ SQL = "INSERT INTO". $ table. "(". $ field. ") VALUES (". $ ins .")";
$ This-> query ($ SQL );
}
Function getinfo ($ table, $ field, $ id, $ colnum) // obtain the detailed information of the current record.
{
$ SQL = "SELECT * FROM". $ table. "WHERE". $ field. "=". $ id ."";
$ Query = $ this-> query ($ SQL );
If ($ this-> fetch_row ($ query ))
{
For ($ I = 1; $ I <$ colnum; $ I ++)
{
$ Info [$ I] = odbc_result ($ query, $ I );
}
}
Return $ info;
}
Function getlist ($ table, $ field, $ colnum, $ condition, $ sort = "order by id DESC") // retrieves the record list
{
$ SQL = "SELECT * FROM". $ table. "". $ condition. "". $ sort;
$ Query = $ this-> query ($ SQL );
$ I = 0;
While ($ this-> fetch_row ($ query ))
{
$ Recordlist [$ I] = getinfo ($ table, $ field, odbc_result ($ query, 1), $ colnum );
$ I ++;
}
Return $ recordlist;
}
Function getfieldlist ($ table, $ field, $ fieldnum, $ condition = "", $ sort = "") // retrieves the record list
{
$ SQL = "SELECT". $ field. "FROM". $ table. "". $ condition. "". $ sort;
$ Query = $ this-> query ($ SQL );
$ I = 0;
While ($ this-> fetch_row ($ query ))
{
For ($ j = 0; $ j <$ fieldnum; $ j ++)
{
$ Info [$ j] = odbc_result ($ query, $ j + 1 );
}
$ Rdlist [$ I] = $ info;
$ I ++;
}
Return $ rdlist;
}
Function updateinfo ($ table, $ field, $ id, $ set) // update record
{
$ SQL = "UPDATE". $ table. "SET". $ set. "WHERE". $ field. "=". $ id;
$ This-> query ($ SQL );
}
Function deleteinfo ($ table, $ field, $ id) // delete a record
{
$ SQL = "DELETE FROM". $ table. "WHERE". $ field. "=". $ id;
$ This-> query ($ SQL );
}
Function deleterecord ($ table, $ condition) // deletes records with specified conditions.
{
$ SQL = "DELETE FROM". $ table. "WHERE". $ condition;
$ This-> query ($ SQL );
}
Function getcondrecord ($ table, $ condition = "") // gets the number of records with specified conditions
{
$ SQL = "SELECT COUNT (*) AS num FROM". $ table. "". $ condition;
$ Query = $ this-> query ($ SQL );
$ This-> fetch_row ($ query );
$ Num = odbc_result ($ query, 1 );
Return $ num;
}
}
?>