简体   繁体   English

将PHP代码从MSSQL迁移到MySQL

[英]Migrate PHP code from MSSQL to MySQL

I currently have a large web application that connects to MSSQL to fetch its data. 我目前有一个大型Web应用程序,该应用程序连接到MSSQL以获取其数据。 This web app has hundreds of .php files. 此网络应用程序包含数百个.php文件。 I have a new customer, and he doesn't want to use MSSQL, but MySQL instead. 我有一个新客户,他不想使用MSSQL,但是要使用MySQL。

I've searched and found lots of articles on how to migrate data. 我搜索并找到了许多有关如何迁移数据的文章。 But that's not my problem. 但这不是我的问题。 I know how to do that, besides we'll be starting with a blank database, except for a few default catalogs. 我知道该怎么做,除了一些默认目录外,我们将从一个空白的数据库开始。

What I need to know is if there's a way to automatically change the mssql_query code to mysqli code. 我需要知道的是,是否有一种方法可以将mssql_query代码自动更改为mysqli代码。 For example: 例如:

$SqlText = 'SELECT a, b, c FROM table WHERE x=y';
if (!$result = mssql_query($SqlText) {
   die('Error');
}
while ($row = mssql_fetch_assoc($result) {
   echo '<td>' . $row['a'] . '</td>';
   echo '<td>' . $row['b'] . '</td>';
   echo '<td>' . $row['c'] . '</td>';
}

Into something like: 变成类似:

$SqlText = 'SELECT a, b, c FROM table WHERE x=y';
if (!$result = mssqli_query($db, $SqlText) {
   die('Error');
}
while ($row = mssqli_fetch_assoc($result) {
   echo '<td>' . $row['a'] . '</td>';
   echo '<td>' . $row['b'] . '</td>';
   echo '<td>' . $row['c'] . '</td>';
}

Besides, as you can see, I was using the old mssql_query code which is not even supported in the newer versions of PHP. 此外,如您所见,我使用的是旧的mssql_query代码,而新版本的PHP甚至都不支持。 Is there at least a tool that could change my code from mssql_query into the new version of sqlserv_query type of commands? 至少有一种工具可以将我的代码从mssql_query更改为新版本的sqlserv_query类型的命令吗?

Any help greatly appreciated. 任何帮助,不胜感激。

I do not think there is a totally automatic way. 我认为没有全自动的方法。 Because MySQL has 'LIMIT' for example where MSSQL has 'TOP'. 例如,因为MySQL具有“ LIMIT”,而MSSQL具有“ TOP”。 Of course that would not have to be a problem but I am not sure if complex MSSQL queries would translate 1:1 to MySQL. 当然,这不一定是问题,但是我不确定复杂的MSSQL查询是否会将1:1转换为MySQL。

And suggest you use the `` signs to point out to MySql it is a member of a databaes, table or column name. 并建议您使用``符号指出MySql,它是数据库,表或列名称的成员。 Example: 例:

$SqlText = 'SELECT a, b, c FROM table WHERE x=y';

Would change to 将更改为

$SqlText = 'SELECT `a`, `b`, `c` FROM `table` WHERE `x`=y';

Now you can make queries like 现在您可以进行查询

'SELECT `select` FROM `datetime` WHERE `limit` = 1'

Otherwise the words, select, datetime and limit would interfere with MySql syntax. 否则,单词select,datetime和limit会干扰MySql语法。

You can use find&replace with your favourit IDE for example, in netbeans click in the project window, then edit, replace and type your function name 您可以在收藏夹的IDE中使用find&replace,例如,在netbeans中,在项目窗口中单击,然后编辑,替换并键入函数名称

You can also use a php script like that : 您还可以使用类似这样的php脚本:

$config = array('msssql_query'=>'mysqli_query' /*... all the functions*/)

$dirs = array('./','subDirectory1','subDirectory2');/*all your sub dirs*/);
foreach($dirs as $dir){
    foreach(glob($dir.'/*.php') as $filename){

         $filecontent = file_get_contents($filename);
         file_put_contents(str_replace($config,$filecontent,$filename));

    }
}

Moreover, as @endyourif said, you'd better wrap your database connexion inside a class and interfaces. 此外,正如@endyourif所说,最好将数据库连接包装在类和接口中。

interface DAOInterface{
     public function query($string);
     public function prepare($string);
     public function quote($string);//etc

}
interface DAOStatementInterface{
    public function execute($array = array());
    public function bind($value,$param,$type);
    /*etc*/
}

or you can use PDO 或者您可以使用PDO

I actually run into this issue recently. 我实际上最近遇到了这个问题。 Found out using MYSQL Workbench did solve the problem of Migrating the database from MSSQL to MYSQL. 发现使用MYSQL Workbench确实解决了将数据库从MSSQL迁移到MYSQL的问题。 However, there remained a problem that while there is not many edits happening in the database, the PHP side codes are being edited frequently due to certain issues. 但是,仍然存在一个问题,尽管数据库中没有进行很多编辑,但是由于某些问题,经常需要对PHP辅助代码进行编辑。 So it made difficult to migrate each files one by one on a daily basis. 因此,很难每天一次迁移每个文件。 So I researched and came up with a function which is still not 100%, and has its disadvantages, but solved my problem. 因此,我研究并提出了一个仍不是100%的函数,它具有缺点,但解决了我的问题。

I used Sublime Text Editor, and using that I replaced all occurrences of mssql_ to sql_, and then included the function. 我使用了Sublime Text Editor,并使用它将所有出现的mssql_替换为sql_,然后包含了该函数。

I tried defining mssql_ functions first, but it turns out, I have to actually disable the mssql from the PHP module. 我尝试首先定义mssql_函数,但事实证明,我实际上必须从PHP模块中禁用mssql。 It is fine if there is only one application using that PHP, but since there are many, I had to rewrite every single pages to new function. 如果只有一个使用该PHP的应用程序很好,但是由于有很多应用程序,我不得不将每个页面重写为新功能。

You can find my code here . 您可以在这里找到我的代码。 But I can't guarantee you get the full result. 但是我不能保证您能得到完整的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM