A method of not converting from SQL Server to MySQL database

Source: Internet
Author: User
Tags ini odbc mssql odbc connection php script sql injection stmt mysql database

At the beginning of the year has been doing a site mssql2000->mysql5 migration work, because the use of different program systems, so the main problem in the data migration. Because 2 system database structure is very big, inconvenient to take SQL Server to import MySQL ODBC data source function (also do not recommend this, field type and so on different points will kill the ~), so in Windows to write their own PHP script from SQL Server read data, Then in accordance with the needs of the new system after the insertion of MySQL inside, flexible and more convenient. The actual process mainly has the following questions:

1, the database connection, the main connection is SQL Server. There are mainly 3 ways:

1.1 Using the Mssql_ series function in PHP, this is similar to using the Mysql_ series function, but to open the associated extension (Extension=php_mssql.dll) in php.ini.

1.2 Using ODBC connection, because of the abstraction of a specific database, so there is no way to use the Data table field name => array key name characteristics, for specific applications is not very convenient, code form:

$conn = odbc_connect("datasource","username","password");
$sql = "select * from news";
$cur= odbc_exec($conn, $sql);
while(odbc_fetch_row($cur)){
$field1 = odbc_result($cur,1);
$field2 = odbc_result($cur,2);
//do something
}

1.3 Use PDO-PHP5 to add data Object Abstraction layer, as the official data access interface, there are many advantages, such as supporting parameter binding to prevent SQL injection, for different databases loaded with different drivers, the program code is consistent, easy to transplant and so on, I believe it should be the general trend. However, due to the use of PHP5 new object-oriented features, the need for PHP5 support, 5.1 can be used directly, 5 need to install PECL, in addition to modify php.ini, add: Extension=php_pdo_mysql.dll and Extension=php_ Pdo_mssql.dll, the actual code is as follows:

try {
$DBH=new PDO("mssql:dbname=XXX;host=localhost",
"root", "password");//Connect to DB
} catch (PDOException $e) {
print "Error!: " . $e->getMessage();//Error Message
die();
}
$stmt = $DBH->prepare("SELECT * FROM news");//Stmt Handle $stmt
if ($stmt->execute()) {
while ($row = $stmt->fetch()) {
//do something
}
}
$stmt2 = $mssql->prepare("INSERT INTO news
(title,author) VALUES (:title, :author)");
$stmt2->bindParam(':title', $title);
$stmt2->bindParam(':author', $author);
$stmt2->execute();
$DBH = null;// Close Connection这里要提醒下的是MSSQL里面是没有MYSQL中LIMIT这个语法的。

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.