Recently received a Web site using Php+access, Khan, has never been done with php+access, reference other people write Access functions, they added some other functions, encapsulated as a class, feel the call is quite convenient, although not tested, but feel php+ Access is not php+mysql faster.
<?php
--------------------------------------------------------------------
FileName:class.php
Summary:access Database Operations Class
Author:forest
Createtime:2006-8-10
Lastmodifed:
Copyright (c) 2006
Http://freeweb.nyist.net/~chairy
Chaizuxue@163.com
Use 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, database connection 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)//Total number of records obtained
{
Return Odbc_num_rows ($this->query ($sql));
}
function close ()//closes the database connection function
{
Odbc_close ($this->link);
}
function inserts ($table, $field)//Insert record functions
{
$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)//Get the details of the 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")//Get a list of records
{
$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 = "")//Get a list of records
{
$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 record
{
$sql = "DELETE from". $table. " WHERE ". $field." = ". $id;
$this->query ($sql);
}
function DeleteRecord ($table, $condition)//delete records for specified criteria
{
$sql = "DELETE from". $table. " WHERE ". $condition;
$this->query ($sql);
}
function Getcondrecord ($table, $condition = "")//number of records to obtain the specified condition
{
$sql = "Select COUNT (*) as num from". $table. " ". $condition;
$query = $this->query ($sql);
$this->fetch_row ($query);
$num =odbc_result ($query, 1);
return $num;
}
}
?>