将大型网站从 MySQL 切换到 MySQLi
我想从 MySQL 切换到 MySQLi,但我有一个非常大的网站.
I want to switch from MySQL to MySQLi, but I have a very large website.
我读到 https://wikis.oracle.com/display/mysql/Converting+to+MySQLi 可以帮助我,我阅读了 我怎样才能把这个mysql改成mysqli?.它说我可以通过在函数中添加一个i"来替换大多数函数,并且我应该开始寻找错误.
I read that https://wikis.oracle.com/display/mysql/Converting+to+MySQLi could help me and I read How could I change this mysql to mysqli?. It says that I could replace most of the functions with just adding an 'i' to the function, and that I should start bughunting.
但我的网站非常复杂且庞大,需要很长时间才能检查一切是否正常.那么:对于一个非常大的网站,从 MySQL 切换到 MySQLi 的最佳方式是什么?
But my website is very complex and large, and it would take a very long time to check if everything works. So: what is the best way to switch from MySQL to MySQLi for a very large website?
谢谢!
推荐答案
您的问题没有简单的答案,因为几乎所有简单的方法都涉及在编写应用程序时以不同的方式做事.
There is no easy answer to your question as practically every simple way to do this involved doing things differently when the application was written.
如果您在整个代码中直接调用 mysql_* 函数,并且没有通过帮助类或函数进行查询的数据库抽象层,那么您将需要编辑每个命令.
If you have direct calls to mysql_* functions throughout your code and no database abstraction layer where you do your queries through a helper class or function then you will need to edit every command.
你不能只是在程序上像 mysql_query 这样的命令中添加一个 i,因为 mysqli_query()
需要第一个参数是到带有 mysql_query()
如果给定了一个连接,它就是第二个参数.
You cannot just get away with adding an i to commands like mysql_query as procedurally mysqli_query()
requires the first parameter to be the link to the db where with mysql_query()
if a connection was given at all, it was a second parameter.
我建议不要只是将 mysql_query(...) 更改为 mysqli_query($link,.....),而现在是放置数据库抽象层的最佳时机.因此,请使用实际处理您的查询的函数,例如 sql_query(),以便将来如果您需要再次更改数据库,您只需更新一个抽象文件中的数据库特定命令.这样,如果您编写一个包装 mysqli_query 的函数,那么您可以简单地将 mysql_query() 重命名为您的辅助函数,并让辅助函数担心将链接放在那里.
Instead of just changing mysql_query(...) to mysqli_query($link,.....) I would recommend that there is no better time to put a db abstraction layer in place. So use functions eg sql_query() that actually process your queries so in future if you need to change DB again you can just update the db specific commands in one abstraction file. That way if you write a function that wraps mysqli_query then you could be able to simply rename your mysql_query() to your helper function and let the helper function worry about putting the link in there.
虽然这是最简单的方法,但它不会绑定参数或准备语句,这是防止 sql 注入漏洞的主要因素
Whilst that is the simplest way, it will not bind parameters or prepare statements which is a major factor in preventing sql injection vulnerabilities
一旦你改变了所有这些你需要测试的命令.
Once you have changed all these commands you need to test.
如果您没有编写自动化测试,那么这可能是开始编写它们的好时机.即使您需要检查每项更改是否有效,但如果您通过自动化测试来完成,那么您将来可以避免这种痛苦.
If you have no automated tests written then this is probably a good time to start writing them. Even though you will need to check that every change has worked, if you do it by automated test then you can avoid that pain in future.
相关文章