PhpSQLserver import Mysql-PHP source code

Source: Internet
Author: User
Ec (2); php script to import data from the user table of mydb database in sqlserver to mydb database in mysql $ cnxodbc_connect (web, admin, 123456 ); web is the data source name of mydb in sqlserver, admin is the user name to access mydb, 123456 is the password to access mydb $ curodbc_exec ($ cnx, script ec (2); script

Php script to import data from the user table of mydb database in SQL server to mydb database in mysql

$ Cnx = odbc_connect ('web', 'admin', '000000'); // 'web' is the data source name of mydb in sqlserver, and 'admin' is the user name for accessing mydb, '123' is the password used to access mydb.
$ Cur = odbc_exec ($ cnx, 'select * from user'); // open the user table of mydb database in SQL server
$ Num_row = 0;
$ Conn = mysql_pconnect ("localhost", "root", "123456"); // connect to mysql
@ Mysql_select_db ('mydb', $ conn) or

Die ("unable to connect to the database, please contact the administrator! "); // Open mysql's mydb Database
While (odbc_fetch_row ($ cur) // extracts data from the user table in the mydb database of SQL server one by one. If you select the data, you can add conditions in the preceding select statement.
{
$ Num_row ++;
$ Field1 = odbc_result ($ cur, 1); // here the parameter I (, 3 ..) it refers to the I-th field in the record set. You can select it selectively. fieldi gets the value of the corresponding field, and then you can perform operations on 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); // converts datetime fields in SQL server to the int type I need.
$ 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;
}
?>

Save the script as SQL. php and execute it on the server to import the data in the user table of mydb database in SQL server to the user table of mydb database in mysql. The operations on other tables are the same as those in other tables.

The two methods use php scripts and asp scripts to import data from the user table from SQL server to mysql. In this case, I use two methods to avoid the transfer of ntext and image data, one is to change the ntext field to nvarchar (4000). In actual conditions, the Data Length of this field in the raw data does not exceed 4000 characters, so no data truncation occurs, another method is to extract the image data to a file, save it as a file, and save the file path to the database. For details, see:

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

This function has three input parameters: the first is the contentType of the file, the second is the binary value of the file, and the third is a variable that can distinguish the file name, determine the suffix of the stored File Based on contentType, and then save the binary value to the specified

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.