php操作ACCESS和SQL数据库示例

记得把php.ini的相关选项打开,如果提示出错,下载adodb,http://adodb.sourceforge.net/

1,操作SQL2005数据库

<?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>&nbsp;<a href="infodetail.php?id=<?php echo $rs->fields['id']->value; ?>"><?php echo $rs->fields['title']->value; ?></a></td>
        <td align="center">&nbsp;<?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>

 

 

二,操作Access

代码 

Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--><?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";

$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>&nbsp;<a href="infodetail.php?id=<?php echo $rs->fields['id']->value; ?>"><?php echo $rs->fields['title']->value; ?></a></td>
        <td align="center">&nbsp;<?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>

 

 

posted @ 2014-03-17 15:07  某人2013  阅读(264)  评论(0编辑  收藏  举报