首页 > 程序开发 > PHP > PHP操作access数据库的部分方法实例
2015
03-21

PHP操作access数据库的部分方法实例

<?php
//读取access数据库数据库案例,很久之前的项目了,存在这里备忘,仅供参考
$conn = new com("ADODB.Connection");
$connstr = "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=". realpath("./SpiderResult.mdb");
$conn->Open($connstr);
$rs = new com("ADODB.RecordSet");
$rs->Open("select * from Content",$conn,1,1);
while(! $rs->eof){
	echo $rs->Fields(1)->value;
	echo "<br>";
	echo $rs->Fields(2)->value;
	echo "<br>";
	echo $rs->Fields(3)->value;
	echo "<br>";
	echo "<hr>";
	$rs->MoveNext();
}

// 数据写入方法
$connstr="DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" . realpath("../chen416/mydata.mdb");
$connid=odbc_connect($connstr,"","",SQL_CUR_USE_ODBC);
$issuetime=date("Y-m-d H:i:s");
$sql="insert into userinfo (username,userword,email) values('u2','w2','uw@163.com')";
$result=odbc_exec($connid,$sql);
if($result) echo "successful";
else echo "failed";

// 读取数据库
$conn=new COM("ADODB.Connection");
$dsn="DRIVER={Microsoft Access Driver (*.mdb)};DBQ=".realpath("../mydata.mdb").";";
$conn->open($dsn);
$sql="select * from userinfo";
$rs=$conn->Execute($sql);
$row="0";
while(!$rs->EOF){
	$string ="";
	for ($i=0;$i<$rs->Fields->count;$i++){
		if ($i==0){
			$string.=$rs->Fields($i)->Value;
			continue;
		}
	$string.='|'.$rs->Fields($i)->Value;
	}

	$string.="\n";
	if ($row==300){
		break;
	}
	$row++;
	echo "<br>";
	echo $string;
	$rs->MoveNext();
}

// 删除数据方法
$conn = new com("ADODB.Connection");
$connstr = "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=". realpath("../mydata.mdb");
$conn->Open($connstr);
$query = "delete from userinfo where uid=1";
$result =$conn->Execute($query);
if($result) echo "删除成功";
else echo "删除失败";

// PHP更新数据方法:
$conn = new com("ADODB.Connection");
$connstr = "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=". realpath("../mydata.mdb");
$conn->Open($connstr);
$query = "update userinfo set username='ganjun' where uid=2";
$result =$conn->Execute($query);
if($result)echo "更新数据成功";
else echo "更新数据失败";

?>
最后编辑日期:
作者:uuling
这个作者貌似有点懒,什么都没有留下。

留下一个回复

你的email不会被公开。