One PHP Access class (PHP + ODBC + Access)

Source: Internet
Author: User
One PHP Access class (PHP + ODBC + Access) The code is as follows:



--------------------------------------------------------------------
// 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 {
$ 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;
}
}
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.