php 我可以盲目地用 mysqli_ 替换所有 mysql_ 函数吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26476162/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can I blindly replace all mysql_ functions with mysqli_?
提问by Midnight Engineer
I have used mysql_query()
throughout my project; but I've just learned that mysql_
was deprecated as of PHP 5.5, has been removed in PHP 7.
我在mysql_query()
整个项目中都使用过;但我刚刚了解到它mysql_
在 PHP 5.5 中已被弃用,已在 PHP 7 中删除。
So, I would like to know if I can replace all mysql_
functions with mysqli_
in my project blindly? For example, just replacing mysql_query()
with mysqli_query()
. Is there any adverse effect?
所以,我想知道我是否可以盲目地替换我项目中的所有mysql_
功能mysqli_
?例如,只需替换mysql_query()
为mysqli_query()
. 有什么不良影响吗?
回答by worldofjr
The short answer is no, the functions are not equivalent.
简短的回答是否定的,功能不等价。
The good news is there is a converter tool that will help you if you've got a lot of calls/projects to change. This will allow your scripts to work right away.
好消息是有一个转换器工具可以帮助你,如果你有很多电话/项目需要改变。这将允许您的脚本立即工作。
https://github.com/philip/MySQLConverterTool
https://github.com/philip/MySQLConverterTool
It's a forked version of the Oracle original version, and it's kosher.
它是 Oracle 原始版本的分叉版本,并且是 kosher 的。
That said, it's not too difficult to update your code, and you might want to migrate to an object orientated methodology anyway ...
也就是说,更新您的代码并不太难,无论如何您可能希望迁移到面向对象的方法......
1) The Connection
1)连接
For all intents and purposes, you need a new connection function that saves the connection as a PHP variable, for example;
出于所有意图和目的,您需要一个新的连接函数,将连接保存为 PHP 变量,例如;
$mysqli = new mysqli($host,$username,$password,$database);
Notice I've saved the connection to $mysqli
. You can save to $db
or whatever you like, but you should use this throughout your code to reference the connection.
请注意,我已将连接保存到$mysqli
. 您可以保存到$db
或任何您喜欢的位置,但您应该在整个代码中使用它来引用连接。
Remember to check for a connection error though;
不过请记住检查连接错误;
if ($mysqli->connect_errno) echo "Error - Failed to connect to MySQL: " . $mysqli->connect_error;
2) The Query
2)查询
Note: You should protect against SQL injection with prepared statements, which are available in MySQLi. Take a look at How can I prevent SQL injection in PHP?, but I'm just going to cover the basics here.
注意:您应该使用 MySQLi 中提供的预处理语句来防止 SQL 注入。看看如何在 PHP 中防止 SQL 注入?,但我将在这里介绍基础知识。
You now have to include the connection as an argument in your query, and other mysqli_
functions. In procedural code it's the first argument, in OO you write it like a class method;
您现在必须将连接作为参数包含在查询和其他mysqli_
函数中。在过程代码中,它是第一个参数,在 OO 中,您可以像类方法一样编写它;
Procedural:
程序:
$result = mysqli_query($mysqli,$sql);
OO:
面向对象:
$result = $mysqli->query($sql);
3) Fetch Result
3) 获取结果
The fetching of the result is similar to the old mysql_
function in procedural;
结果的获取类似于mysql_
程序中的旧函数;
while($row = mysqli_fetch_assoc($result))
but as $result
is now an object in mysqli, you can use the object function call;
但是$result
现在是 mysqli 中的对象,您可以使用对象函数调用;
while($row = $result->fetch_assoc())
4) Close Connection
4) 紧密连接
So as before, you need to include the connection in the close function; as an argument in procedural;
所以和以前一样,你需要在 close 函数中包含连接;作为程序上的论据;
mysqli_close($mysqli);
and as the object that you run the function on in OO;
并作为您在 OO 中运行该函数的对象;
$mysqli->close();
I would be here forever if I went through them all, but you get the idea. Take a look at the documentationfor more information. Don't forget to convert any connection close, result release, or error and row counting functions you have.
如果我经历了所有这些,我会永远在这里,但你明白了。查看文档以获取更多信息。不要忘记转换您拥有的任何连接关闭、结果发布或错误和行计数功能。
The basic rule of thumb is for functions that use the database connection, you need to include it in the function now (either as the first argument in procedural, or the object you use to call the function in OO), or for a result set you can just change the function to mysqli_
or use the result set as the object.
基本的经验法则是对于使用数据库连接的函数,您现在需要将它包含在函数中(作为过程中的第一个参数,或用于在 OO 中调用函数的对象),或者作为结果集您可以将函数更改为mysqli_
或使用结果集作为对象。
回答by MiguelR
If you cannot convert all calls to the mysqli functions on a old project, you could install and include the library php7-mysql-shim.
如果您无法将所有调用转换为旧项目上的 mysqli 函数,您可以安装并包含库php7-mysql-shim。
It will try to create a transparent replacement for mysql on PHP 7 using mysqli. Obviously the performance is slower, but it's a solution to get around the problem in a couple of minutes. You may safely include the library in projects working with PHP 5.6 (it will be ignored).
它将尝试使用 mysqli 在 PHP 7 上为 mysql 创建一个透明的替代品。显然性能较慢,但这是在几分钟内解决问题的解决方案。您可以安全地将库包含在使用 PHP 5.6 的项目中(它将被忽略)。
if (defined('PHP_VERSION_ID') && (PHP_VERSION_ID >= 50600)) { require_once "mysql-shim.php"; }
回答by mokh223
You can't. some of the functions of mysql and mysqli require different parameters. So you should know which will use the same parameters.
你不能。mysql 和 mysqli 的一些函数需要不同的参数。所以你应该知道哪些将使用相同的参数。