PHP Operations Access database Class code

Source: Internet
Author: User

The following is a reference fragment:
The following are the referenced contents:
<?php
Class Access//access Database Operations classes
{
var $databasepath, $constr, $dbusername, $dbpassword, $link;//Properties of Class
function Access ($databasepath, $dbusername, $dbpassword)//constructor
{
$this->databasepath= $databasepath;
$this->username= $dbusername;
$this->password= $dbpassword;
$this->connect ();
}

function connect ()//Database connection functions
{
$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)/Send a query string to the database
{
Return @odbc_exec ($this->link, $sql);
}

function First_array ($sql)//Returns an array from an Access database
{
Return @odbc_fetch_array ($this->query ($sql));
}

function Fetch_row ($query)//Returns a row in the record
{
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 functions
{
$sql = "UPDATE". $table. " SET ". $set." WHERE ". $field." = ". $id;
$this->query ($sql);
}

function Deleteinfo ($table, $field, $id)//delete record functions
{
$sql = "DELETE from". $table. " WHERE ". $field." = ". $id;
$this->query ($sql);
}

function DeleteRecord ($table, $condition)//delete the record functions for the specified condition
{
$sql = "DELETE from". $table. " WHERE ". $condition;
$this->query ($sql);
}

function Getcondrecord ($table, $condition = "")//record number functions that 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;
}
}
?>



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.