简体   繁体   English

通过mysql_连接使用mysqli_函数吗?

[英]Using mysqli_ functions with mysql_ connection?

I have written some code that is using mysqli_ a lot. 我已经写了一些使用mysqli_的代码。 The problem is that I now need to integrate this code with code that is using mysql_ , and takes care of connection, authentication etc. I don't want to create a second connection, but mysql_ lacks a few features, for example, prepared queries. 问题是我现在需要将此代码与使用mysql_代码集成,并负责连接,身份验证等。我不想创建第二个连接,但是mysql_缺少一些功能,例如,准备好的查询。 So I'd like to somehow magically "import" the connection into mysqli . 因此,我想以某种方式神奇地将连接“导入”到mysqli Is this even possible? 这有可能吗? If so, how can that be done? 如果是这样,那该怎么办?

Nope, that's impossible. 不,那是不可能的。

By the way, prepared statements is not the only way to make queries safe. 顺便说一句,准备好的语句并不是使查询安全的唯一方法。
It's placeholders that everyone should use, and they not necessarily have to be implemented using prepared statements. 每个人都应该使用占位符 ,并且不一定必须使用准备好的语句来实现它们。
While placeholders can be easily implemented using mysql_* functions as well 尽管也可以使用mysql_ *函数轻松实现占位符

A connection resource created by mysql_connect can't by reused with the MySQLi extension. mysql_connect创建的连接资源不能与MySQLi扩展一起重用。 You will either have to handle the connection in your current code or refactor the old code. 您将不得不处理当前代码中的连接或重构旧代码。

This is not as hard as it may sound. 这并不像听起来那样难。 MySQLi also has a procedural interface and you can use the extension just like you do with the old MySQL extension. MySQLi还有一个过程接口,您可以像使用旧的MySQL扩展一样使用该扩展。 The biggest benefit of MySQLi is the object oriented interface which would make refactoring a bit more complicated. MySQLi的最大好处是面向对象的接口,它将使重构变得更加复杂。 MySQLi natively also supports (wraps into methods/functions) MySQL features like prepared statements, transactions and connection charset, but this doesn't mean you can't submit a START TRANSACTION or SET CHARSET utf8 just like you did with mysql_query MySQLi本身还支持(包装到方法/函数中)MySQL功能,如预备语句,事务和连接字符集,但这并不意味着您不能像使用mysql_query一样提交START TRANSACTIONSET CHARSET utf8

Hope this helps. 希望这可以帮助。 If you have more specific questions regarding the refactoring feel free to ask :) 如果您有关于重构的更多具体问题,请随时提出:)

As F4r-20 pointed out, you could just change the way you connect to the database and replace the mysql_ functions with msqli_ ones! 作为F4R-20所指出的,你可以只改变你连接到数据库的方式,取代mysql_与功能msqli_的!

Try to decide what you are going to use BEFORE you start a project, not in the middle. 在开始一个项目之前,而不是在中间,尝试决定要使用什么。 Preparing saves a lot of time and issues like this. 准备可以节省大量时间和诸如此类的问题。


PS. PS。 I would use PDO next time. 下次我会使用PDO It is easy, fast, OOP and supports multiple database types! 它简单,快速,面向对象,并且支持多种数据库类型!


Edit: since you don't have access to the credentials or how you connect to the database, you will have to use mysql_ , change the connection to mysqli_ or make a new connection... Sorry :( 编辑:由于您无权访问凭据或连接数据库的方式,因此您将不得不使用mysql_ ,将连接更改为mysqli_或进行新的连接...对不起:(

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

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