PHP ODBC Database operations class __ Database

Source: Internet
Author: User
Tags odbc access database
<?php
//--------------------------------------------------------------------
FileName:class.php
Summary:access Database Operations Class
Createtime:2006-8-10
Lastmodifed:
Copyright (c) 2006 Freeweb.nyist.net/~chairy [Email]chaizuxue@163.com[/email]
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

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.