switch between datebase
-
hi there I am trying to switch between two datbase. I am using
$wpdb->select('databasename'); wp_cache_flush();
however I am not getting any information and the theme is gone and only the text shows on this site.
So I am stuck rigt now
-
Switching databases is like switching sites. Everything that makes a site a site is in the DB. Themes, and WP itself, work based on values stored in the DB. By switching DBs, the data they are relying on has disappeared, leading to unpredictable, unstable behavior. The DBs would need to be carefully synchronized for this to work. Anything site or theme related needs to occur in both DBs. Anytime a setting is changed, it needs to be updated in both DBs. I cannot see such a scheme working well.
Most likely, you would be better off leaving the original DB connection as is and making a new connection using a new
wpdb
class object. Let WP and your theme use the global$wpdb
in the manner that is expected, and use your new connection to do whatever you need from the second DB.If you don’t mind sharing your intentions, someone might be able to suggest a more workable scheme. Why are you trying to switch DBs? What are you trying to accomplish by doing so?
Hi bcworkz and thanks,
I am trying to fetch all coupon and stores from one of my clipper Couponsites to show them on a second Couponsite…..
so i want to switch custom taxonomay tables
I see. Switching DBs was a good thought, but it’s too much of a shortcut. You need to go about this more conventionally. You have several options still.
The easiest is using the new oEmbed capability of v4.4. If you simply place a link to content on the other site in the content of this site (on its own line), a title, excerpt and read more link will be displayed when the post is viewed. What is shown can probably be customized, but I’m not sure how to go about it. oEmbed of other WP sites is a new feature I haven’t looked at in depth yet.
You can still use the
wpdb
class like you already tried, but create a new object to connect to the other DB, do not use the global$wpdb
instance. The issue here is you cannot then use handy WP functions, everything is done through mySQL queries.There are also several APIs that enable you to remotely query the other site for content. There’s XML-RPC capability that’s built in. There’s the WP REST API plugin. And you could install WP-CLI to get a command line interface.
Thanks bcworkz,
i tink oEmbed is more for content like YouTube etc.
The Problem is need certain content from Blog1 in Blog2 (about 100+ shop sides and about 5000 coupons) and the content changes several times a day. So i need to use the other DB. I have a Plugin for multisite thats does the job, however i would like to move the second side into single side…You would be correct about oEmbed if not for WP v4.4. Assuming you have updated to 4.4, your sites are now oEmbed providers in the same way that YouTube, Vimeo, etc. were providers. See for yourself – start a new post in one site and paste the permalink to a post on another v4.4 site. Preview your new post and you’ll see the other site’s post excerpt. No need to publish of course.
While the oEmbed feature will be useful for many, I’m not that convinced it’ll work for you. You’ll need to decide for yourself. I personally would probably go with the new
wpdb
class object approach to connect to the other DB. There’d be a lot of custom code, but since I’m a coder, that is not a deterrent. YMMV. A lot of the required code can be copied from WP source with minor modifications.If I were not a coder, I might be more interested in WP REST API. But you’re not me, you know your skills and the exact needs for your site, which may result in a different approach that is best for you. Choose thoughtfully though, then work up a quick proof of concept example to be sure the approach will actually work before spending much time on it.
Hi and thanks for your input,
i like the Idee of the new wpdb class, however I am still new to programming. So if i switch the database, how do i just get the needed Infos and than switch back?
A new
wpdb
class object is just that, a new DB connection. You are not “switching” databases, you are creating a second connection. It’s as simple as this:global $exdb; $exdb = new wpdb( $dbuser, $dbpassword, $dbname, $dbhost );
WP will continue to use
$wpdb
, the global object. Not to be confused with thewpdb
class. If that remains confusing, it’s OK, you just need to read up on object oriented programming (OOP).In any examples or code where you see
$wpdb
being used, if you substitute our$exdb
, you can do the exact same thing with the external DB. For example, here’s how we could get the number of WP users from the local DB:
$user_count = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->users" );
To get the number of users stored in the external DB, we do this:
$ext_user_count = $exdb->get_var( "SELECT COUNT(*) FROM $exdb->users" );
You will find a lot of WP functions that would be handy to use on the external DB do not work because they use
$wpdb
. You can create your own equivalent function by copying the source code, changing the function name, and changing all instances of$wpdb
to$exdb
.There are likely situations where the code you need to copy is quite extensive. An alternative approach is to actually swap objects, call the extensive code, then immediately swap the objects back before any theme or plugin code executes. For example, we could get posts from the external DB like so:
$original_db = $wpdb; $wpdb = $exdb; $posts = get_posts( $args ); $wpdb = $original_db;
With this approach, you do need to be careful to not call any functions that rely on locally stored data, for example permalink functions rely on the options table to get the domain and path information. You would end up getting the remote site’s permalinks, which might be OK, but if your intention was for a local permalink, then it’s not OK.
Thanks bcworkz,
do you think it is possible to use all this in a plugin? I have a multisite plug in but would like to move one blog out of it and than need to use to switch between databases instead of blogs.
need to read up on this a little more, first I need to solve a more present problem ??Yes, it is all possible via a plugin. It would likely be a plugin unique to your situation, though I imagine that, with some thought, it could be made generic enough for someone else with dual DB needs. “Some thought” often translates to more work, so I’d worry about that part later if you even have any interest at all.
I hate it when real world problems get in the way of “This would be really cool” projects ??
yep it would great to use a plug in and my situation is unique i tried but with no success.
A while ago somebody wrote this plug in for me, for multisite to switch between blogs, now i would like to change it to switch between database, any idea. This is the original code:
/** Plugin Name: Joloshop Central for gutscheincloud.info Plugin URI: https://joloshop.de Description: Uses a central taxonomy source for Coupons & Stores. Version: 1.0 Author: Kai Niermann Author URI: https://joloshop.de License: Private **/ add_action('init', 'central_taxonomies'); add_action('switch_blog', 'central_taxonomies'); function central_taxonomies () { global $wpdb; $wpdb->terms = $wpdb->base_prefix."terms"; $wpdb->term_relationships = $wpdb->base_prefix."term_relationships"; $wpdb->postmeta = $wpdb->base_prefix."postmeta"; $wpdb->term_taxonomy = $wpdb->base_prefix."term_taxonomy"; $wpdb->storesmeta = $wpdb->base_prefix."clpr_storesmeta"; } // ---------------------------------------------------------------- // -------------------- add Blog ID into [ id ] // ---------------------------------------------------------------- class Joloshop_Central { public static $fetch_coupons = [ 2 ]; public static $coupons_blog = 1; public static $fetch_everything = []; public function __construct() { add_action( 'init', [ $this, 'init' ] ); } public function init() { $blog_id = get_current_blog_id(); if ( in_array( $blog_id, self::$fetch_coupons ) ) { $this->switch_taxonomies(); // Coupons are posts. Hook into the pre_get_posts add_action( 'pre_get_posts', [ $this, 'pre_get_posts_coupons' ] ); add_action( 'get_terms_args', [ $this, 'switch_taxonomies' ] ); } if ( in_array( $blog_id, self::$fetch_everything ) ) { } } // ---------------------------------------------------------------- // -------------------- COUPONS // ---------------------------------------------------------------- /** @brief Switch to main blog if we're looking for coupons **/ public function pre_get_posts_coupons( $query ) { // Are we querying for coupons? if ( $query->query_vars[ 'post_type' ] !== 'coupon' ) return $query; // Retrieve the values for the coupons blog $coupons_blog_wpdb = $this->get_other_blog_wpdb( self::$coupons_blog ); // Change posts and postmeta tables. global $wpdb; $wpdb->posts = $coupons_blog_wpdb->posts; $wpdb->postmeta = $coupons_blog_wpdb->postmeta; } public function switch_taxonomies( $p1 = null ) { $coupons_blog_wpdb = $this->get_other_blog_wpdb( self::$coupons_blog ); global $wpdb; $wpdb->terms = $coupons_blog_wpdb->terms; $wpdb->term_relationships = $coupons_blog_wpdb->term_relationships; $wpdb->term_taxonomy = $coupons_blog_wpdb->term_taxonomy; $wpdb->postmeta = $wpdb->base_prefix."postmeta"; $wpdb->storesmeta = $coupons_blog_wpdb->storesmeta; return $p1; } public function get_other_blog_wpdb( $target_blog ) { global $wpdb; switch_to_blog( $target_blog ); $other_blog_wpdb = clone( $wpdb ); restore_current_blog(); return $other_blog_wpdb; } } $joloshop_central = new Joloshop_Central; if ( ! function_exists( 'ddd' ) ) { function ddd() { echo '<pre>'; $r = array(); $args = func_get_args(); foreach( $args as $arg ) $r[] = trim( htmlspecialchars( var_dump( $arg ) ), "'" ); echo implode( ' ', $r ); echo '</pre>'; } } if ( ! function_exists( 'ddds' ) ) { function ddds() { $args = func_get_args(); $output = call_user_func_array( 'sprintf', $args ); return ddd( $output ); } }
You can certainly add on to this existing plugin since it is unique to you site and no third party will push an update. You would start by instantiating a new
wpdb
class object (the 2 lines for$exdb
I posted earlier). Of course you need to define the various parameters fornew wpdb()
– i.e.$dbuser
etc. This establishes a second DB connection, one to the ‘external’ DB, while not disturbing the original DB connection.From there it depends on what you need out of the external DB. It might be easiest to simply run custom mySQL queries using
$exdb
object methods. You could build custom template tags that take simple arguments and use them to build a query, then execute the query, then format and output the results. Then you simply call this template tag from your template.For that matter, you could actually run a
$exdb
query directly from a template since it is a global object. Or, as I mentioned earlier, modify WP functions to use$exdb
instead of$wpdb
. Or actually swap DB connections and use the original WP functions. Just remember to immediately swap them back before other theme or plugin code tries to use the connection. Unless you fully understand all the code executed while the connections are swapped, it’s possible some unanticipated code to execute on the wrong connection, causing the same issues you’ve already experienced. So be careful using this last approach!Hi its me again, I am lost. Do you know of anybody how could help me with this. I just dont get it. What ever i try i dont get the other database ??
if i use
global $exdb $exdb = new wpdb( $dbuser, $dbpassword, $dbname, $dbhost );
do i just have to change
$dbpassword
into
$mypassword
and so on?u can easily do this by simply changing database details in wpconfig , what is the issue in this
if i would change the database than “all” content would be from the other side, but i only need content from some taxonomies.
joloshop, as you know, bukge is confused about what we’re trying to accomplish here. You cannot blame him for not carefully reading all 13 posts ??
When you instantiate a new
wpdb
object you pass all the pertinent connection details as arguments, whether it’s$dbpassword
,$mypassword
or even a literal password like ‘duF%24jnr^1ddU’. The$dbpassword
I provided as an example was just that, an example, one that I copied from the docs. It was not meant to be a copy/paste replacement into your code. Sorry for the confusion.Unfortunately, the help provided in these forums in necessarily limited. A related resource is available on IRC. While it’s also limited, you can get an immediate response, but it’ll be limited to whomever is online at the moment. You could also simply hire someone to handle this for you. jobs.wordpress.net is a good resource.
- The topic ‘switch between datebase’ is closed to new replies.