熟悉asp的人都知道使用ado操作数据库是非常方便的 ,php中也可以使用这个方式。
首先下载adodb,http://adodb.sourceforge.net/,这里的是最新的,另外也可以【点击这里下载】,这个是2010年8月15日下载下来的。
如果你写过asp程序,那使用起php的adodb来操作access或者sql server一点障碍都没有,简直一模一样。
其中的分页是qdodb已经实现了的,对于中小型数据量的程序来讲不用过多的考虑,如果对大数据量的分页就要考虑更科学的分页方法了。
下面是php,adodb,操作access的示例代码:
<!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>
<title></title>
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body>
<div align="center">
<?php
$sql = "select * from info order by createtime desc";
$conn = new com("ADODB.Connection");
$connstr = "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" . realpath("qzcddb.mdb");
if($conn->State == 0)
$conn->Open($connstr);
$rs = new com("ADODB.RecordSet");
$rs->Open($sql,$conn,1,3);
if($rs->recordcount == 0)
{
echo "无记录";
}
else
{
$rs->pagesize=5;
$page = $_REQUEST['page'];
if((trim(intval($page))=='')||(intval($page)>$rs->pagecount)||(intval($page)<=0))
{
$page=1;
}
else
{
$page=intval($page);
}
$rs->absolutepage=$page;
$mypagesize=$rs->pagesize;
?>
<table width="600" border="1" cellspacing="0" cellpadding="0">
<tr align="center">
<td width="6%" height="30"><strong>编号</strong></td>
<td><strong>文章标题</strong></td>
<td width="16%"><strong>建立时间</strong></td>
</tr>
<?php
for ($i = 0; $i < $rs->pagesize; $i++)
{
?>
<tr>
<td height="30" align="center"><?php echo $rs->fields['id']->value; ?></td>
<td> <a href="infodetail.php?id=<?php echo $rs->fields['id']->value; ?>"><?php echo $rs->fields['title']->value; ?></a></td>
<td align="center"> <?php echo $rs->fields['createtime']->value; ?></td>
</tr>
<?php
$rs->movenext();
if($rs->eof)break;
}
?>
</table>
<table height="50">
<tr>
<td></td>
<td width="100" align="center">当前<?php echo $page;?>/<?php echo ceil($rs->recordcount/$rs->pagesize);?>页</td>
<td>
[<a href="lista.php?page=1">首页</a>]
[<a href="lista.php?page=<?php echo $page-1;?>">上一页</a>]
[<a href="lista.php?page=<?php echo $page+1;?>">下一页</a>]
[<a href="lista.php?page=<?php echo ceil($rs->recordcount/$rs->pagesize);?>">尾页</a>]
</td>
<td width="80" align="center">共<?php echo $rs->recordcount;?>条记录</td>
<td>
<input type="text" name="page" id="page" value="<?php echo $page;?>" style="width: 50px; height: 25px">
</td>
<td><input type="button" name="button" id="button" value="跳转" onClick="location.href='lista.php?page='+document.getElementById('page').value" /></td>
</tr>
</table>
<?php
}
$rs->close;
$conn->close;
?>
</div>
</body>
</html>
下面是php,adodb,操作sql server2005的示例代码:
<?php include("inc/adodb.inc.php"); ?>
<!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>
<title></title>
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body>
<div align="center">
<?php
$sql = "select * from info order by createtime desc";
$connstr = "PROVIDER=MSDASQL;DRIVER={SQL Server};SERVER={CG\SQLSERVER2005};DATABASE=db;UID=sa;PWD=123;";
$conn = new com("ADODB.Connection");
if($conn->State == 0)
$conn->Open($connstr);
$rs = new com("ADODB.RecordSet");
$rs->Open($sql,$conn,1,3);
if($rs->recordcount == 0)
{
echo "无记录";
}
else
{
$rs->pagesize=5;
$page = $_REQUEST['page'];
if((trim(intval($page))=='')||(intval($page)>$rs->pagecount)||(intval($page)<=0))
{
$page=1;
}
else
{
$page=intval($page);
}
$rs->absolutepage=$page;
$mypagesize=$rs->pagesize;
?>
<table width="600" border="1" cellspacing="0" cellpadding="0">
<tr align="center">
<td width="6%" height="30"><strong>编号</strong></td>
<td><strong>文章标题</strong></td>
<td width="16%"><strong>建立时间</strong></td>
</tr>
<?php
for ($i = 0; $i < $rs->pagesize; $i++)
{
?>
<tr>
<td height="30" align="center"><?php echo $rs->fields['id']->value; ?></td>
<td> <a href="infodetail.php?id=<?php echo $rs->fields['id']->value; ?>"><?php echo $rs->fields['title']->value; ?></a></td>
<td align="center"> <?php echo $rs->fields['createtime']->value; ?></td>
</tr>
<?php
$rs->movenext();
if($rs->eof)break;
}
?>
</table>
<table height="50">
<tr>
<td></td>
<td width="100" align="center">当前<?php echo $page;?>/<?php echo ceil($rs->recordcount/$rs->pagesize);?>页</td>
<td>
[<a href="list.php?page=1">首页</a>]
[<a href="list.php?page=<?php echo $page-1;?>">上一页</a>]
[<a href="list.php?page=<?php echo $page+1;?>">下一页</a>]
[<a href="list.php?page=<?php echo ceil($rs->recordcount/$rs->pagesize);?>">尾页</a>]
</td>
<td width="80" align="center">共<?php echo $rs->recordcount;?>条记录</td>
<td>
<input type="text" name="page" id="page" value="<?php echo $page;?>" style="width: 50px; height: 25px">
</td>
<td><input type="button" name="button" id="button" value="跳转" onClick="location.href='list.php?page='+document.getElementById('page').value" /></td>
</tr>
</table>
<?php
}
$rs->close;
$conn->close;
?>
</div>
</body>
</html>
其中
$connstr = "PROVIDER=MSDASQL;DRIVER={SQL Server};SERVER={CG\SQLSERVER2005};DATABASE=db;UID=sa;PWD=123;";
需要解释一下,“CG\SQLSERVER2005”是数据库服务器的地址,对于数据库安装时是默认实例的可以只写机器名称或者ip,如CG或者192.168.0.1,本机可以使用“.”或者127.0.0.1,对于自定义了实例的数据库就需要再写上实例的名称,如SQLSERVER2005。
代码中<?php include("inc/adodb.inc.php"); ?>是包含了“adodb.inc.php”文件。
“adodb.inc.php”是下载下来的adodb中的文件,需要将该文件复制到站点下面。