Access操作正确
时间:2010-11-05 来源:hopingphp
第一种方法:
--------------------------------------------------------------------------------
<?php
class Access{
function getConn(){
$conn = @new COM("ADODB.Connection") or die ("ADO连接失败!");
$connstr = "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" . realpath(__IMPL__."/db2.mdb");
$conn->Open($connstr);
return $conn;
}
}
?>
操作:
写:
$sql="insert into stu (name) values ('$name')";
$rs = $conn->Execute($sql);
读:
$rs=$conn->Execute('select * from stu');
while(!$rs->EOF){
$id=$rs->fields['id']->value;
$name=$rs->fields['name']->value;
$rs->movenext();
}
$rs->close();
$conn->close();
改:
$sql="update stu set age='".$age."' where id=".$id;
$rs=$conn->Execute($sql);
$conn->close();
删:
$sql="delete from stu where id=".$id;
$rs = $conn->Execute($sql);
$conn->close();
--------------------------------------------------------------------------------
方法二:
<?php
class Access1{
function getConn(){
$connstr="DRIVER=Microsoft Access Driver (*.mdb);DBQ=".realpath(__IMPL__."/db2.mdb");
$connid=odbc_connect($connstr,"","",SQL_CUR_USE_ODBC );
return $connid;
}
}
?>
操作:
$sql="insert into stu (name) values ('$name')";
$query=odbc_do($connid,$sql);
while(odbc_fetch_row($query))
{
$name = odbc_result($query,2);
echo $name."<br>";
}
其他SQL语句同上。
注意事项:PHP路径问题是个非常麻烦的问题,事先定义目录变量 define()
<?php
define('__ROOT_DIR__', dirname(__FILE__));
define('__IMPL__',dirname(__FILE__)."/impl");
define('__ACTION__',dirname(__FILE__)."/action");
define('__UTIL__',dirname(__FILE__)."/util");
define('__SMARTY__',dirname(__FILE__)."/util/libs");
?>