Home > Backend Development > PHP Tutorial > PHP读取ACCESS数据库实例代码

PHP读取ACCESS数据库实例代码

WBOY
Release: 2016-06-20 13:01:27
Original
1081 people have browsed it

PHP读取ACCESS数据库实例代码

<html>

<head></head>

<body>

<?php

/**

功能:测试PHP链接ACCESS数据库

环境:WINXP+ACCESS2000+PHP4.3.4 for win32+apache2.0.48(win32)

*/



//数据源,其中的Data Source="D:\NJDM.mdb"为数据库绝对地址,相对地址可以自己试试

$MM_Conn_STRING="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\Projects\NJDM\dm2.mdb";

$Conn=new com("ADODB.Connection");

$RS =new com("ADODB.RecordSet");//数据集

$Conn->open($MM_Conn_STRING);//链接打开数据源

$query="select * from dm order by id asc";

$RS->open($query,$Conn,1,1);//执行语句,返回数据集

echo"共有:".$RS->RecordCount."条记录结果!<br>";//纪录数

for ($i=0;$i<$RS->fields->Count;$i++)

echo $RS->fields[$i]->Name." ";//遍历字段名



echo "<br>";

$connect = mysql_connect("192.168.0.51", "username", "password");

while(!$RS->EOF)

{

$query = "insert into smsGames.nanjing_toponym values('";

$query .= $RS->fields["id"]->value."', '";

$query .= $RS->fields["name"]->value."', '";

$query .= $RS->fields["symbol"]->value."', '";

$query .= str_replace("\r\n", "", $RS->fields["weizhi"]->value)."', '";

$query .= $RS->fields["way"]->value."', '";

$query .= $RS->fields["class"]->value."', '";

$query .= str_replace("\r\n", "", $RS->fields["history"]->value)."', '";

$query .= str_replace("\r\n", "", $RS->fields["addr"]->value)."')";

$result = mysql_query($query, $connect);

if (!$result)

print "Error:".mysql_error()."<br>Query:".$query."<br>";

else

print $RS->fields["id"]->value."  ";

$RS->MoveNext();

$i++;

}



//释放资源

$Conn->Close();

$RS=NULL;

$Conn =NULL;

?>

</body>

</html>
Copy after login

 


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