简体   繁体   中英

Using mysqli_ functions with mysql_ connection?

I have written some code that is using mysqli_ a lot. 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. So I'd like to somehow magically "import" the connection into 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

A connection resource created by mysql_connect can't by reused with the MySQLi extension. 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. The biggest benefit of MySQLi is the object oriented interface which would make refactoring a bit more complicated. 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

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!

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. I would use PDO next time. 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 :(

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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