The easiest way to link ACCESS database with PHP

巴扎黑
Release: 2016-11-24 14:36:42
Original
1466 people have browsed it

<?PHP
     //创建ADO连接
$conn = @new COM("ADODB.Connection") or die ("ADO连接失败!");
$connstr = "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" . realpath("temp/TempData.mdb");
$conn->Open($connstr);
     //创建记录集查询
$rs = @new COM("ADODB.RecordSet");
$rs->Open("select * from blog_Content",$conn,1,3);
echo $rs->Fields["log_Title"]->Value;     //输出log_Title字段
echo "<br/>";
$rs->Movenext();     //将记录集指针下移
echo $rs->Fields["log_Title"]->Value;
$rs->close(); 
?>
Copy after login

This is just a simple method. We can also connect using Microsoft.Jet.OLEDB.4.0, which will be faster.
Note: conn = @new COM("ADODB.Connection") or die ("ADO connection failed!");
It should be noted that COM must be in uppercase letters. Previously, lowercase letters were used in Windows XP, and illegal operations occurred. Use it here. There is an @ symbol, its main function is fault tolerance!
Other database operation methods are similar to ASP, so I won’t introduce them in detail~

二·
Let’s make a comparison using ASP and PHP together...

ASP program

Program code
1.conn= "DRIVER=Microsoft Access Driver (*.mdb);DBQ=" & Server.MapPath("*.mdb")
2.set conn = server.createobject("adodb.connection" )
3.conn.open conn
4.set rs = Server.CreateObject("adodb.recordset")
5.sql = "select * from table name"
6.rs.Open sql, conn, 3, 1
7.rs.Close

PHP program

Program code

<?PHP
1.$conn = @new COM("ADODB.Connection") or die ("ADO连接失败!");
2.$connstr = "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" . realpath("temp/TempData.mdb");
3.$conn->Open($connstr);
4.$rs = @new COM("ADODB.RecordSet");
5.$sql ="select * from blog_Content";
6.$rs->Open($sql,$conn,1,3);
7.$rs->close(); 
?>
Copy after login

两个程序都相对应....很好理解了吧.嘿....然后就是ASP和PHP操作记录集了...有什么不同,,看资料了事5~
php: $rs[name/index];                      
asp:   rs(name/index);                      
php: $rs->Fields["name"]->Value / fields[index]->Value;
asp:   rs.fields(index)/rs.field(name);     
php中的方法和ASP中基本一致,只是写法上不一样,,注意:PHP区分大小写,写时后面一定要加;号                 

asp:
修改记录集中的记录 
rs.AddNew               向记录集中添加一条新记录
rs.Delete                  从记录集中删除一条记录
rs.{fieldName/fieldIndex}=指定值;
rs.Update                保存对当前记录所做的修改
CancelBatch            当记录集处在批量更新模式时)取消一批更新
CancelUpdate         调用Update之前)取消对当前记录所做的所有修改                             
UpdateBatch           当记录集处于批量更新模式时)保存对一个或多个记录的修改
遍历记录集
Move NumRecords    在记录集中向前或向后移动指定数目的记录数。
MoveFirst                  移动到记录集的第一条记录
MoveNext                  移动到记录集的下一条记录
MovePrevious            移动到记录集中的上一条记录
MoveLast                   移动到记录集的最后一条记录
记录集对象属性
AbsolutePosition        用来设置或读取当前记录在记录集中的位置顺序号
BOF                            标明当前位置在记录集中的第一条记录之前
EOF                            标明当前位置在记录集中的最后一条记录之后
RecordCount              表示一个记录集中的记录总数
记录集进行分页
AbsolutePage             指定当前的页
PagePount                 返回记录集中的逻辑页数
PageSize                     指定一个逻辑页中的记录个数,缺省值是10
GetRows()                   记录取到数组
MaxRecords                 记录集的最大容量 

Related labels:
php
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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!