AG积分

座機: 029-81773686 熱線: 18966895139
網頁制作、網絡營銷、維護推廣、手機網站制作一站式解決方案
十分感謝六年來與我們攜手共進的朋友們、客戶們!
php讀取Access數據庫并寫入mysql數據庫
添加時間:2014-10-28  點擊率:3403次
<?php $conn = new com("ADODB.Connection"); $connstr = "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=". realpath("11.mdb"); $conn->Open($connstr); $rs = new com("ADODB.RecordSet"); $rs->Open("select * from newslist where n_ppid=81",$conn,1,3); while(!$rs->eof) { $title=$rs->Fields(4); $neirong=$rs->Fields(8); $time=$rs->Fields(24); $laiyuan=$rs->Fields(10); $zuozhe=$rs->Fields(11); $sql=mysql_query("INSERT INTO mysql數據庫名字(title,content,addtime,laiyuan,author)<br /> VALUES('".$title."','".$neirong."','".$time."','".$laiyuan."','".$zuozhe."')"); $rs->MoveNext(); } ?>

以上php讀取Access數據庫信息和錄入mysql是不是沒有錯誤呢?但是很多時候是導入不成功的,最常見的問題就是兩個數據庫的編碼不同,怎么辦呢,假如access數據庫是gbk的,mysql數據庫是utf8的,我們做以下操作即可:

<?php $conn = new com("ADODB.Connection"); $connstr = "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=". realpath("11.mdb"); $conn->Open($connstr); $rs = new com("ADODB.RecordSet"); $rs->Open("select * from newslist where n_ppid=81",$conn,1,3); while(!$rs->eof) { $title=mb_convert_encoding($rs->Fields(4), 'utf-8','gb2312' ); $neirong=mb_convert_encoding($rs->Fields(8), 'utf-8','gb2312' ); $time=mb_convert_encoding($rs->Fields(24), 'utf-8','gb2312' ); $click=mb_convert_encoding($rs->Fields(12), 'utf-8','gb2312' ); $laiyuan=mb_convert_encoding($rs->Fields(10), 'utf-8','gb2312' ); $zuozhe=mb_convert_encoding($rs->Fields(11), 'utf-8','gb2312' ); $sql=mysql_query("INSERT INTO mysql數據庫名字(title,content,addtime,laiyuan,author)<br /> VALUES('".$title."','".$neirong."','".$time."','".$laiyuan."','".$zuozhe."')"); $rs->MoveNext(); } ?>
?
版權所有 ? AG积分宏博網絡,并保留所有權利
客服中心

AG积分


展開客服