第一种:oledb链接
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>php链接Access数据库</title>
</head>
<body>
<?php
//使用php com类链接Access数据库 [oledb连接]
$con=new com("adodb.connection") or die("链接失败");
$constr="DRIVER={Microsoft Access Driver (*.mdb)};DBQ=".realpath("datdb.mdb"); //链接字符串,realpath:返回绝对路径
$con->Open($constr); //打开库
//echo $con->state(); //连接状态:1连接成功
$rs=$con->execute("select * from mtab order by id desc");
//执行sql语句
/*while(!$rs->eof){ //不是最后一条记录就循环
$id=$rs->fields['id']; //取表中字段
$name=$rs->fields['姓名'];
//$addr=$rs->fields['地址'];
echo 'ID:'.$id->value.'<br/>';
echo '姓名:'.$name->value.'<br/>'; //输出字段的值
$rs->movenext(); //每次指针下移
}
*/
?>
<table width="300" border="1" align="center" cellpadding="0" cellspacing="0">
<tr>
<td align="center">编号</td>
<td align="center">姓名</td>
</tr>
<?php
while(!$rs->eof){
$id=$rs->fields['id'];
$name=$rs->fields['姓名'];
?>
<tr>
<td align="center"><?php echo $id->value;?></td>
<td align="center"><?php echo $name->value;?></td>
</tr>
<?php
$rs->movenext();
}
$rs->close();
$rs=null;
$con->close(); //使用完关闭记录集和链接
$con=null;
?>
</table>
</body>
</html>
第二种:odbc链接:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>php连接Access</title>
</head>
<body>
<?php
//php连接Access数据库 使用odbc链接
$constr="DRIVER={Microsoft Access Driver (*.mdb)};DBQ=".realpath("datdb.mdb");
$con=odbc_connect($constr,"","",SQL_CUR_USE_ODBC) or die("链接错误");//产生连接,SQL_CUR_USE_ODBC:记录集类型,四种类型:1.SQL_CUR_USE_IF_NEEDED、2.SQL_CUR_USE_ODBC、3.SQL_CUR_USE_DRIVER、4.SQL_CUR_DEFAULT;主要确定能否操作记录数据
$rs=odbc_do($con,"select * from mtab order by id desc"); //执行sql语句
/*while(odbc_fetch_row($rs)){
$id=odbc_result($rs,1);
$name=odbc_result($rs,'姓名');
echo $id.'<br/>';
echo $name.'<br/>';
}
*/
?>
<table width="300" border="1" align="center" cellpadding="0" cellspacing="0">
<tr>
<td align="center">编号</td>
<td align="center">姓名</td>
</tr>
<?php
while(odbc_fetch_array($rs)){ //取记录
$id=odbc_result($rs,1); //去字段,可使用数字或字段名
$name=odbc_result($rs,'姓名');
?>
<tr>
<td align="center"><?php echo $id;?></td>
<td align="center"><?php echo $name;?></td>
</tr>
<?php
}
?>
</table>
</body>
</html>
-------------------------------------------------------------------------------------------------------------------
评论