How to use mysqli in WordPress

2019-09-05 22:22发布

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+ queries 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`);

Thanks!!

1条回答
再贱就再见
2楼-- · 2019-09-05 22:43

You're right to want to learn to use MySQL with php from the ground up. You can use libraries and shortcuts and optimizations when you appreciate how and when to use them. Here's an example of how straightforward basic msqli can be:

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

                    // Check connection
                    if (mysqli_connect_errno())
                      {
                      echo "Failed to connect to MySQL: " . mysqli_connect_error();
                      }

                    // Perform queries
                    mysqli_query($con,"SELECT * FROM Persons");
                    mysqli_query($con,"INSERT INTO Persons (FirstName,LastName,Age)
                    VALUES ('Glenn','Quagmire',33)");

                    mysqli_close($con);
        ?> 

If you're comfortable with objects this is a good tutorial: http://codular.com/php-mysqli but this one is a bit fuller and will give you a very good grounding in PHP-MySQL: https://www.binpress.com/tutorial/using-php-with-mysql-the-right-way/17

查看更多
登录 后发表回答