简体   繁体   English

查找/替换mysql_到mysqli_使代码无效

[英]Find/replace mysql_ to mysqli_ makes code nonfunctional

So as I'm sure anyone who's been a regular on SO has noticed, the mysql_ functions are going to be deprecated and it is suggested that one use mysqli_ or PDO instead. 因此,我确信任何曾经是SO的常规人员已经注意到, mysql_函数将被弃用,并建议使用mysqli_PDO代替。 Thus, I decided to transition over to mysqli by doing a simple find/replace in my code, replacing every mysql_ with mysqli_ . 因此,我决定通过在我的代码中进行简单的查找/替换来转换到mysqli,用mysql_替换每个mysqli_ This seemed to work okay in Dreamweaver, and I got no syntax errors or anything. 这似乎在Dreamweaver中工作正常,我没有语法错误或任何东西。 All the new mysqli_ functions were colored blue, meaning that they were recognized as valid functions. 所有新的mysqli_函数都是蓝色的,这意味着它们被识别为有效函数。

However, when I saved everything and ran the code, I noticed that any code having to do with mysql had become nonfunctional. 但是,当我保存所有内容并运行代码时,我注意到任何与mysql有关的代码都无法运行。 Undoing the replace solved the problem. 撤消替换解决了问题。

Is my server perhaps not supporting the mysqli functions? 我的服务器可能不支持mysqli功能吗? I know that it's running php 5 and mysql 5. Is there perhaps something else that you have to add to the code? 我知道它正在运行php 5和mysql 5.是否还有其他东西需要添加到代码中? What am I missing? 我错过了什么?

It's a good time to switch now, since PHP 7.0 removed the ext/mysql functions from core. 这是现在切换的好时机,因为PHP 7.0从核心删除了ext/mysql函数。

Consider the following legacy code 请考虑以下遗留代码

$res = mysql_query($sql);

The mysql_ functions were lazy, in that it would use whatever connection was open. mysql_函数是懒惰的,因为它会使用任何打开的连接。 But mysqli_ is not only not lazy, it's an object , not a connection resource (which is what mysql_connect() would return). mysqli_不仅不是懒惰,它是一个对象 ,而不是连接资源(这是mysql_connect()将返回的)。 Look at these lines here for connecting to the database 在这里查看这些行以连接到数据库

$mysqli = new mysqli('host', 'username', 'password', 'db');
$mysqli = mysqli_connect('host', 'username', 'password', 'db');

Both of them give you the same thing... 他们俩都给你同样的东西......

Returns an object which represents the connection to a MySQL Server. 返回表示与MySQL服务器的连接的对象

Yes, that's right. 恩,那就对了。 Even the procedural connection function returns an object. 甚至程序连接函数也返回一个对象。 The simplest way to use it is directly in is object form 最简单的使用方法是直接使用对象形式

$mysqli->query($sql);

The procedural functions are simply wrappers for this. 程序功能只是包装器。 But they are NOT lazy. 但他们并不懒惰。 Check out the difference 看看差异

mysqli_query($mysqli, $sql);

The single largest gotcha in this (why a simple find and replace of mysql_ with mysqli_ fails) is that most of the mysqli_ functions require you to pass the connection object as the first argument . 这个中最大的问题(为什么mysql_的简单查找和替换与mysqli_失败)是大多数mysqli_函数都要求你将连接对象作为第一个参数传递 It's also important to note that results are objects as well ( mysqli_result class), although their procedural counterparts only require the result set so you can simply find/replace those. 同样重要的是要注意结果也是对象( mysqli_result类),尽管它们的过程对应物只需要结果集,因此您可以简单地查找/替换它们。

Just to be complete, there's one other gotcha that's less common (this is really old code): mysql_result . 只是为了完成,还有一个不常见的问题(这是非常古老的代码): mysql_result This function has no equivalent in mysqli_ . 这个函数在mysqli_没有等价物。 You should switch to the full row return functions like mysqli_fetch_assoc($result) 你应该切换到整行返回函数,如mysqli_fetch_assoc($result)

你可能想看看http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers ,它应该告诉你关于迁移mysql_代码的所有知识,但是要告诉你PDO,而不是MySQLi ..

you Have to know There are many issues might had be done when you were replacing your code. 您必须知道在更换代码时可能会遇到许多问题。

  1. Check the installation of mysqli here . 这里检查mysqli的安装。
  2. check the configuration of mysqli here . 这里查看mysqli的配置。
  3. check the DB connection in tow ways : 以两种方式检查数据库连接:

    ** simple connection like natimysql: ** natimysql之类的简单连接:

    $con = mysqli_connect("localhost","my_user","my_password","my_db");

    But Now You have to use it like that 但是现在你必须这样使用它

    mysqli_query($con,"SELECT * FROM Table"); // $con is the connection which is must for executing the sql query

    **my best way is dealing with it as new mysqli object like that : **我最好的方法是将它作为新的mysqli对象处理:

    $con = new mysqli('localhost', 'user', 'pass', 'demo');

    now you will do it like that : 现在你会这样做:

     $sql = "SELECT * FROM Table"; $con->query($sql); 

    if you checked every query that you had done before You must find one of these errors specially the errors in the 3rd point you can take a tour in its tutorial here , here or here 如果你每次检查之前,你必须找到这些错误专门的错误在第三点,你可以采取参观,在教程的一个,你做了询问这里这里这里

Surprised nobody has mentioned that the mysql_ and mysqli_ functions are not exact parallels, therefore a search and replace is bound to fail. 很惊讶没人提到mysql_和mysqli_函数不是完全相似的,因此搜索和替换必然会失败。 Also mysqli_ will generally insist on the database connection parameter (when used in procedural mode, which is what you're aiming fo), where mysql_ would just grab whatever connection had been opened earlier in the code. 此外,mysqli_通常会坚持使用数据库连接参数(当在程序模式中使用时,这是你的目标),其中mysql_只会抓取代码中先前打开的任何连接。

If you have debugging enabled, you should be able to work through the errors. 如果启用了调试,则应该能够解决错误。

The most common 'gotcha' is that there is no mysqli equivalent of mysql_result(), which people often used when getting a single result. 最常见的“问题”是没有mysqli等效的mysql_result(),人们在获得单个结果时经常会使用它。 I'd be willing to bet your code is scattered with "mysqli_result()" calls, which will all fail. 我愿意打赌你的代码散布着“mysqli_result()”调用,这些都会失败。

That's the basic fix. 这是基本的解决方案。 The 'good' fix might be to use a dedicated class for all your SQL calls, so that there are only a handful of uses of the mysqli functions in your entire codebase, and all in a single file. “好”修复可能是为所有SQL调用使用专用类,因此在整个代码库中只有少数使用mysqli函数,并且所有函数都在一个文件中。 That way when you refactor you don't need to search through mountains of files all with a variety of different uses of various mysqli functions. 这样,当你重构时,你不需要搜索大量文件,所有文件都具有各种mysqli函数的各种不同用法。

If you "Need some authoritative sources for MySQL -> MySQLi migration" I can recommend a website called Google ;-) Type in "MySQL -> MySQLi migration". 如果您“需要MySQL的一些权威来源 - > MySQLi迁移”我可以推荐一个名为Google的网站;-)输入“MySQL - > MySQLi迁移”。

For instance, this 'for Dummies' page shows you the differences between the various functions that you'll need to address to get your code working. 例如,这个“for Dummies”页面显示了您需要解决的各种功能之间的差异,以使您的代码正常工作。

http://www.dummies.com/how-to/content/how-to-convert-mysqli-functions-to-mysql-functions.html http://www.dummies.com/how-to/content/how-to-convert-mysqli-functions-to-mysql-functions.html

mysql_ functions are now deprecated mysql_函数现已弃用

Nope, they aren't yet 不,他们还没有

I decided to transition over to mysqli by doing a simple find/replace in my code 我决定通过在我的代码中进行简单的查找/替换来转换到mysqli

the problem most likely lies in the fact that mysqli functions has reverse parameters order and require connection resource explicitly. 问题很可能在于mysqli函数具有反向参数顺序并且需要显式地连接资源。

Nevertheless, even if you succeed with such a bulk replace, it will do not a slightest good to your code , so, you'd better keep it as is, despite of some errr... overenthusiastic campaign on this site. 尽管如此,即使你成功进行大量替换,它对你的代码也没有丝毫的好处 ,所以,你最好保持原样,尽管在这个网站上有一些错误的......过于激烈的竞选。

  1. There is no reason to hurry. 没有理由赶时间。 You have at least 5 years until you'd start notice some inconvenience. 你有至少5年的时间,直到你开始注意到一些不便。
  2. The goal of all this noise about mysql functions is not for changing some API to another but for making usual PHP codes slightly more sensible and safe. 关于mysql函数的所有这些噪音的目标不是将一些API更改为另一个API,而是为了使通常的PHP代码更加明智和安全。 You won't reach neither of these goals with just search and replace - so, there is no reason to do it at all. 只搜索和替换你不会达到这两个目标 - 所以,根本没有理由这样做。

What you really need is a some intelligent way of handling queries, encapsulated in some library class, which will not only improve your experience with running SQL queries but also will move all the API functions into one place where they can be easily replaced to whatever API some folks will decide to force you to use. 你真正需要的是一种处理查询的智能方法,封装在一些库类中,这不仅可以提高运行SQL查询的体验,还可以将所有API函数移动到一个可以轻松替换为任何API的地方。有些人会决定强迫你使用。

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

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