简体   繁体   English

我可以盲目地用 mysqli_ 替换所有 mysql_ 函数吗?

[英]Can I blindly replace all mysql_ functions with mysqli_?

I have used mysql_query() throughout my project;我在整个项目中都使用了mysql_query() but I've just learned that mysql_ was deprecated as of PHP 5.5, has been removed in PHP 7.但我刚刚了解到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?所以,我想知道是否可以在我的项目中盲目地将所有mysql_函数替换为mysqli_ For example, just replacing mysql_query() with mysqli_query() .例如,只需将mysql_query()替换为mysqli_query() Is there any adverse effect?有什么不良影响吗?

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 .请注意,我已将连接保存到$mysqli You can save to $db or whatever you like, but you should use this throughout your code to reference the connection.您可以保存到$db或任何您喜欢的内容,但您应该在整个代码中使用它来引用连接。

Remember to enable error reporting for mysqli before opening the connection;记得在打开连接之前为mysqli启用错误报告;

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

2) The Query 2)查询

Note: You should protect against SQL injection with prepared statements, which are available in MySQLi.注意:您应该使用 MySQLi 中提供的预处理语句来防止 SQL 注入。 Take a look at How can I prevent SQL injection in PHP?看看如何在 PHP 中防止 SQL 注入? , but I'm just going to cover the basics here. ,但我将在这里介绍基础知识。

You now have to include the connection as an argument in your query, and other mysqli_ functions.您现在必须将连接作为参数包含在查询和其他mysqli_函数中。 In procedural code it's the first argument, in OO you write it like a class method.在过程代码中,它是第一个参数,在 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;所以和以前一样,你需要在 close 函数中包含连接; as an argument in procedural;作为程序上的论据;

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 documentation for 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_或使用结果集作为对象。

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.它将尝试使用 mysqli 在 PHP 7 上为 mysql 创建一个透明的替代品。 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).您可以安全地将库包含在使用 PHP 5.6 的项目中(它将被忽略)。

if (defined('PHP_VERSION_ID') && (PHP_VERSION_ID >= 50600)) { require_once "mysql-shim.php"; }

You can't.你不能。 some of the functions of mysql and mysqli require different parameters. mysql 和 mysqli 的一些函数需要不同的参数。 So you should know which will use the same parameters.所以你应该知道哪些将使用相同的参数。

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

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