本文實例講述了PHP把MSSQL數(shù)據(jù)導(dǎo)入到MYSQL的方法距潘。分享給大家供大家參考翁涤。具體分析如下:
最近需要把一個以前的asp網(wǎng)站轉(zhuǎn)換成php的,但php是與mysql而我的asp與mssql的,結(jié)果就需要把mssql數(shù)據(jù)導(dǎo)入到mysql數(shù)據(jù)庫了,下面我自己寫了一個實例還抄了一個實例都不錯.
實例一,代碼如下:
<?php
//國內(nèi)的PNR碼連接
$hostname="127.0.0.1"; //MSSQL服務(wù)器的IP地址 或 服務(wù)器的名字
$dbuser="sa"; //MSSQL服務(wù)器的帳號
$dbpasswd="sa"; //MSSQL服務(wù)器的密碼
$dbname="aa"; //數(shù)據(jù)庫的名字
$conn = mssql_connect($hostname,$dbuser,$dbpasswd); //連接MSSQL
mssql_select_db($dbname); /*連接要訪問的數(shù)據(jù)庫 這里也可以寫做 $db=mssql_select_db($dbname,$conn); */
$sql = "select * from Sheet1$"; //sql語句
$data = mssql_query($sql); //把查詢的值集合在變量$data
while($Arr = mssql_fetch_object($data)) //循環(huán)初始的集合$Arr
{
$Airport=$Arr->Airport;
$citycode=$Arr->citycode;
$Chinesecityname=$Arr->Chinesecityname;
$Chinesecityjp=$Arr->Chinesecityjp;
$english=$Arr->english;
$countrycode=$Arr->countrycode;
$countryfullname=$Arr->countryfullname;
$Chauname=$Arr->Chauname;
//echo $code;
$conn=mysql_connect("localhost","root","123456");//連接數(shù)據(jù)庫的帳號和端口號
mysql_query("SET NAMES 'GBK'",$conn);
mysql_select_db("taojipiao2009",$conn);// 加載數(shù)據(jù)庫
//$sql="update internationcode set jp='$aa' where Code='$Code'";
$sql="insert into internationcode(Airport,citycode,Chinesecityname,Chinesecityjp,english,countrycode,countryfullname,Chauname) values('$Airport','$citycode','$Chinesecityname','$Chinesecityjp','$english','$countrycode','$countryfullname','$Chauname')";
//echo $sql."<br>";
$result=mysql_query($sql);
}
//mssql_close($conn); //關(guān)閉數(shù)據(jù)庫
?>
參考代碼二,代碼如下:
<?php
$mssql_link = mssql_connect($db_host,$db_msuser,$db_mspass) or die("mssql數(shù)據(jù)庫連接失敗");
mssql_select_db($db_msname,$mssql_link);
$mysql_link = mysql_connect($db_myhost,$db_myuser,$db_mypass) or die("mysql數(shù)據(jù)庫連接失敗".mysql_error());
mysql_select_db($db_myname,$mysql_link);
$msquery = mssql_query("select top 1 * from buyok_produc",$mssql_link);
$vars = '';
$vals = '';
$cols = '';
while ($row = mssql_fetch_array($msquery,$mssql_link)){
$vals = '';
foreach($row as $key=>$values){
$cols .= ($cols == '' ? $key : ','.$key);
$vals .= ($vals == '' ? '\''.$values.'\',' : '\''.$values.'\',');
//echo $vals;
}
$vars .= ($vars == '' ? '('.$vals.')' : ',('.$vals.')');
}
$sql = "insert into `buyok_produc` ($cols) values $vars";
echo $sql;
$aa=mysql_query($sql, $mysql_link);
if ($aa){
echo "successfully";
}else{
echo "failed";
}
?>