How to use mysqli in WordPress

I need to know how to use mysqli in WordPress. I don’t want to use wpdb or PDO any thing else. Actually the issue is there are 190+ quries written in mysql and i need to convert them to mysqli, i know the better solution is to go with wpdb. But it would be more time taking. So i just i need to know how to use mysqli in WordPress. I have googled it lot but every where every one is suggesting to use wpdb, but i want to stick with mysqli. So right now my code is e.g :

mysql_query("select * from wp_users);
if i write in mysqli it would be

mysqli_query($HOW_TO_GIVE_CONNECTION_HERE,"select * from wp_users);

That should work.

But you can’t always get away with just adding an ‘i’ to the end of ‘mysql’, there are differences.

Actually, i want to know to give connection string here? $HOW_TO_GIVE_CONNECTION_HERE

Is there any way ti get it from wpdb or any thing else?

Wordpress supports mysqli out of the box. In the wpdb class there is an option to turn it on. – line 1464

What you’re suggesting is a massive hack… not surprising from the Wordpress crowd – they tend to never do things the right way anyway which is why Wordpress is likely used in the first place.

mysqli is used by default by WordPress provided PHP 5.5 or greater is being used.

I can understand that you might be frustrated with WordPress, @oddz, and I don’t want to get this thread off-topic but you shouldn’t really use such a blanket statement as this unless you can justify it. It is unfair to the many developers who work with WordPress who are conscientious about the way they do things.

Why don’t we just try to constructively help the OP with his question.


Unless the OP has written their own database calls (in which case WordPress has nothing to do with it and they’ll need to fix them just as if WordPress wasn’t being used) then they don’t need to worrry about the database calls as WordPress runs on all supported versions of PHP using mySQLi for all its database calls. It only continues to support the older calls for people still running it on long dead versions of PHP.

I will never ever post my question here. Its much easy to go off-topic rather then to stick on topic. Anways thanks all and good bye.

I’m sorry you feel that way.

I guess it depends on what ones considers “on-topic” to be.

You know the better thing is to change the code to use $wpdb.
But I don’t know why you feel that changing from one poor choice to another would take less time than doing it the better way and being done with it, never to worry about needing to do it again later when / if mysqli_ gets phased out.

Despite how some feel about WordPress, it does have a very thorough Codex and if that fails you there is a large number of WordPress coders that can help.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.