热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

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

Ihaveusedmysql_query()throughoutmyproject;butIvejustlearnedthatmysql_wasdeprecatedas

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();但我刚刚了解到,自PHP PHP起,mysql_已被弃用,已在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?

所以,我想知道我是否可以盲目地用我的项目中的mysqli_替换所有mysql_函数?例如,只需用mysqli_query()替换mysql_query()。有不良影响吗?

3 个解决方案

#1


37  

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原始版本的分叉版本,它是犹太洁食。

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:

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 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_或将结果集用作对象。

#2


5  

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"; }

#3


2  

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的一些功能需要不同的参数。所以你应该知道哪些将使用相同的参数。


推荐阅读
  • 本章将深入探讨移动 UI 设计的核心原则,帮助开发者构建简洁、高效且用户友好的界面。通过学习设计规则和用户体验优化技巧,您将能够创建出既美观又实用的移动应用。 ... [详细]
  • 本文详细介绍了如何在Linux系统上安装和配置Smokeping,以实现对网络链路质量的实时监控。通过详细的步骤和必要的依赖包安装,确保用户能够顺利完成部署并优化其网络性能监控。 ... [详细]
  • 优化ListView性能
    本文深入探讨了如何通过多种技术手段优化ListView的性能,包括视图复用、ViewHolder模式、分批加载数据、图片优化及内存管理等。这些方法能够显著提升应用的响应速度和用户体验。 ... [详细]
  • 本文详细介绍了 GWT 中 PopupPanel 类的 onKeyDownPreview 方法,提供了多个代码示例及应用场景,帮助开发者更好地理解和使用该方法。 ... [详细]
  • 本文将介绍如何编写一些有趣的VBScript脚本,这些脚本可以在朋友之间进行无害的恶作剧。通过简单的代码示例,帮助您了解VBScript的基本语法和功能。 ... [详细]
  • Explore how Matterverse is redefining the metaverse experience, creating immersive and meaningful virtual environments that foster genuine connections and economic opportunities. ... [详细]
  • Explore a common issue encountered when implementing an OAuth 1.0a API, specifically the inability to encode null objects and how to resolve it. ... [详细]
  • PHP 5.2.5 安装与配置指南
    本文详细介绍了 PHP 5.2.5 的安装和配置步骤,帮助开发者解决常见的环境配置问题,特别是上传图片时遇到的错误。通过本教程,您可以顺利搭建并优化 PHP 运行环境。 ... [详细]
  • 1.如何在运行状态查看源代码?查看函数的源代码,我们通常会使用IDE来完成。比如在PyCharm中,你可以Ctrl+鼠标点击进入函数的源代码。那如果没有IDE呢?当我们想使用一个函 ... [详细]
  • 本文详细介绍了 Dockerfile 的编写方法及其在网络配置中的应用,涵盖基础指令、镜像构建与发布流程,并深入探讨了 Docker 的默认网络、容器互联及自定义网络的实现。 ... [详细]
  • 本文详细介绍了如何使用 Yii2 的 GridView 组件在列表页面实现数据的直接编辑功能。通过具体的代码示例和步骤,帮助开发者快速掌握这一实用技巧。 ... [详细]
  • 前言--页数多了以后需要指定到某一页(只做了功能,样式没有细调)html ... [详细]
  • 资源推荐 | TensorFlow官方中文教程助力英语非母语者学习
    来源:机器之心。本文详细介绍了TensorFlow官方提供的中文版教程和指南,帮助开发者更好地理解和应用这一强大的开源机器学习平台。 ... [详细]
  • 构建基于BERT的中文NL2SQL模型:一个简明的基准
    本文探讨了将自然语言转换为SQL语句(NL2SQL)的任务,这是人工智能领域中一项非常实用的研究方向。文章介绍了笔者在公司举办的首届中文NL2SQL挑战赛中的实践,该比赛提供了金融和通用领域的表格数据,并标注了对应的自然语言与SQL语句对,旨在训练准确的NL2SQL模型。 ... [详细]
  • 数据库内核开发入门 | 搭建研发环境的初步指南
    本课程将带你从零开始,逐步掌握数据库内核开发的基础知识和实践技能,重点介绍如何搭建OceanBase的开发环境。 ... [详细]
author-avatar
放开丶那手
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有