我可以盲目地用mysqli_替换所有mysql_函数吗? [英] Can I blindly replace all mysql_ functions with mysqli_?

查看:22
本文介绍了我可以盲目地用mysqli_替换所有mysql_函数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在整个项目中都使用了 mysql_query();但我刚刚了解到 mysql_ 自 PHP 5.5 起已被弃用,已在 PHP 7 中删除.

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_函数替换为mysqli_?例如,只需将 mysql_query() 替换为 mysqli_query().有什么不良影响吗?

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?

推荐答案

简短的回答是,功能不等价.

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

它是 Oracle 原始版本的分叉版本,并且是 kosher 的.

It's a forked version of the Oracle original version, and it's 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) 联系

出于所有意图和目的,您需要一个新的连接函数,例如将连接保存为 PHP 变量;

For all intents and purposes, you need a new connection function that saves the connection as a PHP variable, for example;

$mysqli = new mysqli($host, $username, $password, $database);

请注意,我已将连接保存到 $mysqli.您可以保存到 $db 或任何您喜欢的内容,但您应该在整个代码中使用它来引用连接.

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报错;

Remember to enable error reporting for mysqli before opening the connection;

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

2) 查询

注意:您应该使用 MySQLi 中可用的预处理语句来防止 SQL 注入.看看如何防止 PHP 中的 SQL 注入?,但我将在这里介绍基础知识.

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_ 函数中.在过程代码中,它是第一个参数,在 OO 中,您可以像类方法一样编写它.

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.

程序:

$result = mysqli_query($mysqli, $sql);

面向对象:

$result = $mysqli->query($sql);

3) 获取结果

结果的获取与procedural中旧的mysql_函数类似;

The fetching of the result is similar to the old mysql_ function in procedural;

while ($row = mysqli_fetch_assoc($result))

但是由于 $result 现在是 mysqli 中的一个对象,您可以使用对象函数调用;

but as $result is now an object in mysqli, you can use the object function call;

while ($row = $result->fetch_assoc())

4) 关闭连接

所以和以前一样,你需要在close函数中包含连接;作为程序中的论据;

So as before, you need to include the connection in the close function; as an argument in procedural;

mysqli_close($mysqli);

并作为您在 OO 中运行该函数的对象;

and as the object that you run the function on in 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.

基本的经验法则是对于使用数据库连接的函数,您现在需要将它包含在函数中(作为过程中的第一个参数,或者您在 OO 中用于调用函数的对象),或者对于一个结果集,您可以将函数更改为 mysqli_ 或将结果集用作对象.

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.

这篇关于我可以盲目地用mysqli_替换所有mysql_函数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆