php access 数据库转换到mysql数据库代码
本人采集来的数据是ACCESS的,但我的程序是mysql的,故写了一个程序,程序三步走,分享一下,喜欢操作数据库的朋友应该都会用到的.
1.连接mysql,代码如下:
- $dbhost="localhost:3307";
- $db="dataname";
- $dbuser="root";
- $dbpass="*******";
- $db_qianzhui="sdb_";
- $link=mysql_connect($dbhost,$dbuser,$dbpass);
- if(!$link) echo "链接失败!";
- else echo "成功!!!";
- mysql_query("SET NAMES 'UTF8'");
- mysql_query("SET CHARACTER SET UTF8");
- mysql_query("SET CHARACTER_SET_RESULTS=UTF8'");
- mysql_select_db("{$db}");
2.连接access数据库,代码如下:
- $datapath="data.mdb";
- $fenlei="101";
- $conn = new com("ADODB.Connection");
- $connstr = "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=". realpath($datapath);
- $conn->Open($connstr);
- $rs = new com("ADODB.RecordSet");
- $rs->Open("select * from information where lanmu3id=13 ",$conn,1,1);
3.以下准备一边读一写入操作:
- while(! $rs->eof) {
-
-
- $id =iconv ('gb2312','utf-8',$rs->Fields(ID)->value);
- $name = iconv ('gb2312','utf-8',$rs->Fields(title)->value);
- $jianjie = iconv ('gb2312','utf-8',$rs->Fields(1)->value);
- $content= iconv ('gb2312','utf-8',$rs->Fields(content)->value);
- $down = iconv ('gb2312','utf-8',$rs->Fields(download)->value);
-
- echo "<font color=red>".$name."</font>已经插入<br>";
- $sql="INSERT INTO `shopex`.`sdb_articles` (`article_id`,`node_id`,`title`,`content`,`uptime`,`ifpub`,`align`,`filetype`,`filename`,`orderlist`,`disabled`) VALUES ('','{$fenlei}','{$name}','{$info}','1275549287','1',Null,Null,NULL,NULL,'false')";
-
- mysql_query($sql);
-
- $rs->MoveNext();
-
- }