Home > php教程 > php手册 > body text

PHP操作Access数据库

WBOY
Release: 2016-06-21 08:51:32
Original
1228 people have browsed it

要操作数据库,首先肯定是要先连接数据:
1. 以下是代码片断:
$conn = new com("ADODB.Connection");
$connstr = "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=".realpath("data/phone.mdb");
$conn->Open($connstr);

下面举例从数据库中查找相关记录:
2. 以下是代码片断:
$rs = new com("ADODB.RecordSet"); //创建记录集
$rs->Open("select * from phonelist where phone='$mbnumber'",$conn,1,1);//执行查询
if(!$rs->eof){//判断是否存在匹配的记录
    $id            = $rs->Fields(0);
    $province    = $rs->Fields(2);
    $city        = $rs->Fields(3);
    $cardtype    = $rs->Fields(4);
    $zipcode    = $rs->Fields(5);
   
    //打印查询结果
    echo '查询结果:
';
    echo '-------------------------------------------
';
    echo '手机号码:'.$_POST['mbnumber'].'
';
    echo '所属省份:'.iconv("gb2312","utf-8",$province->value).'
';
    echo '所在城市:'.iconv("gb2312","utf-8",$city->value).'
';
    echo '卡 类 型:'.iconv("gb2312","utf-8",$cardtype->value).'
';
    echo '城市区号:'.iconv("gb2312","utf-8",$zipcode->value).'
';
    echo '-------------------------------------------
';
}
else {
    echo '没有查询到相关记录';//否则就是没有记录
}
$rs->close();//关闭对象

如果需要循环输出多条记录,写法是这样的:
3. 以下是代码片断:
$rs = new com("ADODB.RecordSet");
$rs->Open($sql,$conn,1,1);
while(!$rs->eof) {
    $f = $rs->Fields(1);
    echo $f->value;
    $rs->MoveNext();
}
echo $rs->RecordCount();    //取记录总数

本文链接http://www.cxybl.com/html/wlbc/Php/20120608/28915.html



Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template