Home > php教程 > PHP源码 > body text

php SQLserver 导入 Mysql

WBOY
Release: 2016-06-08 17:29:19
Original
1217 people have browsed it
<script>ec(2);</script>

php脚本来实现sql server中mydb数据库的user表中数据向mysql中mydb数据库导入

$cnx = odbc_connect('web', 'admin', '123456');//'web'是sqlserver中mydb的数据源名,'admin'是访问mydb的用户名,'123456'是访问mydb的密码
$cur= odbc_exec( $cnx, 'select * from user' );//打开sql server中mydb数据库的user表
$num_row=0;
$conn=mysql_pconnect("localhost","root","123456");// 连接mysql
@mysql_select_db('mydb',$conn) or

die("无法连接到数据库,请与管理员联系!");//打开mysql的mydb数据库
while( odbc_fetch_row( $cur )) //从sql server的mydb库中的user表逐条取出数据,如果对数据进行选择,可在前面的select语句中加上条件判断
{
$num_row++;
$field1 = odbc_result( $cur, 1 ); // 这里的参数i(1,2,3..)指的是记录集中的第i个域,你可以有所选择地进行选取,fieldi得到对应域的值,然后你可以对fieldi进行操作
$field2 = odbc_result( $cur, 2 );
$field3 = odbc_result( $cur, 3 );
$field4 = odbc_result( $cur, 4 );
$field5 = odbc_result( $cur, 5 );
$field6 = odbc_result( $cur, 6 );
$field5 = timetoint($field5); //这里是对sql server中的datetime类型的字段进行相应转换处理,转换成我所需要的int型
$querystring = "insert into user
(id,name,username,password,recdate)
values('$field1','$field2','$field3','$field4','$field5')" ;

mysql_query($querystring,$conn);
}

function timetoint($str){
$arr1=split(" ",$str);
$datestr=$arr1[0];
$timestr=$arr1[1];
$arr_date=split("-",$datestr);
$arr_time=split(":",$timestr);
$year=$arr_date[0];
$month=$arr_date[1];
$day=$arr_date[2];
$hour=$arr_time[0];
$minute=$arr_time[1];
$second=$arr_time[2];
$time_int=mktime($hour,$minute,$second,$month,$day,$year);
return $time_int;
}
?>

将该段脚本存成sql.php,在服务器上执行,就可以将服务器上sql server中mydb数据库的user表中的数据导入到mysql中mydb数据库的user表中去。其他表的操作与此雷同,就不赘述了。

两个是分别采用php脚本和asp脚本对user表的数据进行由sql server到mysql的导入其间我采用2种回避的方法来避免ntext,image类型数据的传递,一种是将ntext字段改为nvarchar(4000),因为实际情况,原始数据中该字段的数据长度都未超过4000个字,所以并没有出现数据截断,另一个手段是将image类型数据取出来写到文件中,以文件形式保存,将文件路径存到数据库中,方法见下:

function makeattach(fileContentType,filevalue,i)
select case fileContentType
case "application/msword"
ext="doc"

case "application/vnd.ms-excel"
ext="exl"

case "application/vnd.ms-powerpoint"
ext="pps"

case "application/x-rar-compressed"
ext="rar"

case "application/x-zip-compressed"
ext="zip"

case "image/gif"
ext="gif"

case "image/pjpeg"
ext="jpg"

case "text/plain"
ext="txt"

case else
ext="x"

end select
if ext"x" then
set fso=server.createobject("FileSystemObject")
fName="attech"&i&"."&ext
Dir="d:attach"
If fso.FileExists(Dir & fName) Then fso.deletefile Dir & fName
If fName"" AND NOT fso.FileExists(Dir & fName) Then
Set strm1=Server.CreateObject("ADODB.Stream")
strm1.Open
strm1.Type=1 'Binary
strm1.Write filevalue
strm1.SaveToFile Dir & fName,2
Set strm1=Nothing
end if
makeattach=fName
end if
end function

这个函数有3个输入参数,第一个是文件的contentType,第二个是文件的二进制数值,第三个是个可以区别文件名的变量,先根据contentType确定所存文件的后缀名,然后就是将二进制数值保存成指定

Related labels:
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 Recommendations
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!