PHP SQL Server Import Mysql

Source: Internet
Author: User
Tags mysql in php and php script rar

PHP script to implement MYDB database import into MySQL in the user table of SQL Server MyDB database

-->

$CNX = Odbc_connect (' web ', ' admin ', ' 123456 ');//' web ' is the MyDB data source name in SQL Server, ' admin ' is the username to access mydb, ' 123456 ' is the password to access MyDB
$cur = odbc_exec ($CNX, ' select * from user ');//Open MyDB Database User table in SQL Server
$num _row=0;
$conn =mysql_pconnect ("localhost", "root", "123456");//Connect MySQL
@mysql_select_db (' MyDB ', $conn) or

Die ("Unable to connect to the database, please contact the Administrator!") ")//Open MySQL's mydb database
while ($cur)//Odbc_fetch_row from the user table in the SQL Server's MyDB library, if you select the data, you can add conditional judgments to the previous SELECT statement
{
$num _row++;
$field 1 = Odbc_result ($cur, 1); Here's the parameter I (1,2,3 ...) Refers to the first domain in the recordset, where you can selectively select, Fieldi get the value of the corresponding field, and then you can manipulate the Fieldi
$field 2 = Odbc_result ($cur, 2);
$field 3 = Odbc_result ($cur, 3);
$field 4 = Odbc_result ($cur, 4);
$field 5 = Odbc_result ($cur, 5);
$field 6 = Odbc_result ($cur, 6);
$field 5 = timetoint ($field 5); Here is the corresponding conversion of the DateTime type fields in SQL Server to the type I need int
$querystring = "INSERT INTO user
(id,name,username,password,recdate)
VALUES (' $field 1 ', ' $field 2 ', ' $field 3 ', ' $field 4 ', ' $field 5 ') ";

mysql_query ($querystring, $conn);
}

function Timetoint ($STR) {
$arr 1=split ("", $str);
$DATESTR = $arr 1[0];
$TIMESTR = $arr 1[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;
}
?>

By saving the script as a sql.php and executing on the server, you can import the data from the user table of the MYDB database in SQL Server on the servers into the user table in the MyDB database in MySQL. The other table's operations are similar to this, do not repeat.

Two are using PHP script and ASP script to the user table data from SQL Server to MySQL in the meantime I used 2 avoidance methods to avoid the ntext,image type of data transfer, one is to change the ntext field to nvarchar (4000), Because the actual situation, the data length of the original data in this field is not more than 4,000 words, so there is no data truncation, another means is to remove the image type data to write to the file, save as a file, the file path to the database, method see below:

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 3 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, and then, based on the ContentType, determines the suffix name of the stored file and then saves the binary value to the specified

Related Article

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.