ACCESS数据操作类
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<?php
class db_sql
{
function connect($dbhost = '', $dbuser, $dbpw, $dbname = '')
{
$constr="DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" . realpath($dbhost.$dbname);
$this->link=odbc_connect($constr,$dbuser,$dbpw);
}
function fetch_array($query)
{
return odbc_fetch_array($query);
}
function query($sql) {
return @odbc_exec($this->link,$sql);
}
//echo $db->insert("tb",array(users=>"aaa"));
function insert($table, $bind=array()){
$set = array();
foreach ($bind as $col => $val) {
$set[] = "$col";
$vals[] = "'$val'";
}
$sql = "INSERT INTO ".$table.' (' . implode(', ', $set).') '.'VALUES (' . implode(', ', $vals).')';
$result = $this->query($sql);
//$insert_id = $this->insert_id();
//if ($insert_id) $result = $insert_id;
return $result;
}
//$db->update("tb",array(users=>"ab",sex=>"woman"),"sex='man'");
function update($table, $bind=array(),$where = ''){
$set = array();
foreach ($bind as $col => $val) {
$set[] = "$col = '$val'";
unset($set[$col]);
}
$sql = "UPDATE ".$table.' SET '.implode(',', $set).($where ? " WHERE $where" : '');
return $this->query($sql);
}
//$db->del("tb","id='2'");
function del($table,$where = '')
{
$sql = "delete from ".$table.($where ? " WHERE $where" : '');
return $this->query($sql);
}
//--只取第一条记录
function get_one($sql) {
$query=$this->query($sql,'U_B');
$rs =& odbc_fetch_array($query);
return $rs;
}
function affected_rows()
{
return odbc_fetch_row();
}
function insert_id()
{
$id = $this->query("SELECT @@IDENTITY;");
return $id;
}
function error()
{
return odbc_error();
}
function result($query, $row)
{
$query = @odbc_result($query, $row);
return $query;
}
//-- 结果集中字段数
function num_fields($query)
{
return odbc_num_fields($query);
}
//-- 释放结果内存,仅需要在考虑到返回很大的结果集时会占用多少内存时调用。
function free_result($query)
{
odbc_free_result($query);
}
function fetch_row($query)
{
$query = $this->query($query);
$query = odbc_fetch_row($query);
return $query;
}
function close()
{
return odbc_close($this->link);
}
function halt($message = '', $sql = '') {
$dberror = $this->error();
if($message) {
echo "<strong>BxCms! info</strong>: $message<br>";
}
echo "<br><strong>The URL Is:</strong> http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']."?".$_SERVER['QUERY_STRING']."<br>";
if($sql) {
echo "<br><strong>SQL</strong>: ".htmlspecialchars($sql)."<br>";
}
echo "<br><strong>Error</strong>: ".$dberror."<br>";
exit();
}
function GetDataSet($sql)
{
$arr = array();
$query = $this->query($sql);
while($row=$this->fetch_array($query))
{
array_push($arr,$row);
}
return $arr;
}
}
?>