dbareis Posted October 14 Share Posted October 14 Hi, I'm currently using a second "wpdb" object to connect to a MariaDB database. Google gemini says this is not recommended because: Singleton Pattern: The wpdb object is designed to be a singleton, meaning there's only one instance of it per request. Creating a second instance can disrupt this pattern and lead to unexpected results. Resource Management: WordPress's built-in resource management system is optimized for a single wpdb instance. Creating a second instance can complicate resource management and potentially lead to issues like connection timeouts or memory leaks. Compatibility: WordPress's core functionality and many plugins and themes rely on the wpdb object. Introducing a second instance can interfere with these dependencies and cause compatibility problems. I don't know what a singleton is so is what Gemini said true? I've seen many examples where they recommend a second wpdb object (in a second variable). If it is OK, do I need to close this second connection myself? If so what is the best way? I'm assuming the open is an expensive operation. If I need to use "mysqli_connect", same question about opening/closing. I'm looking at my use of "wpdb" because of these PHP error messages (and 500 & 503 server responses): [14-Oct-2024 02:56:44 UTC] PHP Warning: mysqli_real_connect(): (HY000/1203): User xxxusernamexxx already has more than 'max_user_connections' active connections in /home/yyyy/public_html/wp-includes/class-wpdb.php on line 1982 [14-Oct-2024 03:30:30 UTC] PHP Warning: mysqli_real_connect(): (HY000/2006): MySQL server has gone away in /home/yyyy/public_html/wp-includes/class-wpdb.php on line 198 Now the website is low on resources and as a NFP organisation (which I volunteer at), getting it better resourced is problematic. So the above could be due to lack of RAM. But when it goes bad it is bad for a long time. I suspect the timing of web crawlers is the main cause if load is the issue and I have have to turn them all off in "robots.txt"... Thanks for any help :-) Quote Link to comment https://forums.phpfreaks.com/topic/324926-connect-via-wordpress-wpdb-to-another-database-vs-mysqli_connect/ Share on other sites More sharing options...
Barand Posted October 14 Share Posted October 14 If both databases are on the same server then you only need a single connection (A connection is made to the server, not a specific database. The database name in the connect function is just the default to use). This allows you you access two databases in a single query. Specify the database.tablename when referencing the tables. Suppose you want to copy tableA from DB1 to DB2... CREATE TABLE DB2.tableA LIKE DB1.tableA; INSERT INTO DB2.tableA SELECT * FROM DB1.tableA; If DB1 is the default, it can be omitted EG INSERT INTO DB2.tableA SELECT * FROM tableA; Quote Link to comment https://forums.phpfreaks.com/topic/324926-connect-via-wordpress-wpdb-to-another-database-vs-mysqli_connect/#findComment-1637780 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.