PHP access operations

Source: Internet
Author: User

CopyCode The Code is as follows: <? PHP
--------------------------------------------------------------------
// Filename: class. php
// Summary: Access database operation class
// Author: Forest
// Createtime: 2006-8-10
// Lastmodifed:
// Copy right (c) 2006
// Http://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 = "") // obtain the number of records for a specified condition
{< br> $ SQL = "select count (*) as num from ". $ table. "". $ condition;
$ query = $ this-> query ($ SQL);
$ this-> fetch_row ($ query );
$ num = odbc_result ($ query, 1);
return $ num;
}< BR >?>

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.