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

查看:109
本文介绍了我可以用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原始版本的分叉版本,并且是犹太洁食.

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.

请记住要检查连接错误;

Remember to check for a connection error though;

if ($mysqli->connect_errno) echo "Error - Failed to connect to MySQL: " . $mysqli->connect_error;

2)查询

注意:您应该使用准备好的语句来防止SQL注入,这些语句在MySQLi中可用.看一看如何防止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);

OO:

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

3)提取结果

获取结果类似于过程中的旧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天全站免登陆