Using a "variable"

I apologize in advance if this is a simple question that I should just know, but I really haven't run into this before, so I'm going to pose it here hoping someone can offer a quick answer when you get a chance.

Is there a way to have a "variable" in a WPMultiSite page that can pull information from the respective underlying MySQL table associated with the specific subsite? To explain better, I have set up this MultiSite application where when you put the information in the main site (the Deal), it mirrors the content to the designeated subsites (basically a copy of the deal). Each of the subsites will basically be an affiliate site dedicated to the specific affiliate promoting that "deal" on that subsite.

So on subsite "A" that may be for a group called PGP and they would be an affiliate for every deal that gets pushed to their subsite. I can go to each subsite and modify the "BUY" button link so that it handles their respective affiliate code properly, BUT, if I could just have a variable in the main site that when mirrored to the subsite would insert the affiliate code associated with that vendor for that subsite, the my job would be so much easier. Otherwise, I'll have to visit each subsite to add the affiliate codes.

I had considered writing some custom PHP to operate on the MySQL database and handle all of this - and I may still take that approach - but if there's a simple way to do it while the number of affiliates is low, I would love a simple way to get it going and then work on the more complex PHP code to better automate it.

The other day, I just powered up Navicat and went straight to the tables to take care of it.

Thanks in advance for the help/suggestions.


  • 3SixtyEvolve
    • New Recruit

    Hi JDD

    Greetings from the WPMU DEV Support Team and thank you for being a member of our community.

    Navicat is definitely a powerful tool to use when dealing with databases. Now I haven't yet dealt with variables and databases on the level you're doing it, so I won't be able to give you much feedback in that respect. However, I found a few good online articles that might be useful to you.

    I will also flag another support team member that might be able to assist you. Until then here goes the links:

    PHP MySQL Tutorial
    Basics of extracting data from MySQL using PHP
    MySQL: How to pull information from multiple tables based on information in other tables?
    PHP/MYSQL: Pulling information from database

    If there links are not useful or way below your level of knowledge, I apologize in advance :slight_smile: Just thought I would try to help, while I flag my colleague.

    All the best and have a good day!


  • Barry
    • DEV MAN’s Mascot

    If you global $wpdb then that always points to the database of the blog that it is being run on. You can then use the switch_to_blog and restore_current_blog functions to move to point it at a different blog when you need to run a query or operation on that database. E.G.

    function get_data_from_blog( $blog_id, $sql) {
    global $wpdb;
    switch_to_blog( $blog_id );
    $data = $wpdb->get_results( $sql );
    return $data;
  • James Dunn
    • The Crimson Coder

    Thanks for all the responses here and I'll read through each of them and study the links given. However, I decided to add a little supplemental information in case I didn't give enough before that may have made your suggested solutions much more complex than they have to be.

    This is a "Deals" type website (much like a Groupon, but for a specific niche). The main site (ex: is where all the deals are put in and there's no affiliates on that website (or at least none planned at this point). If someone wants to be an affiliate for the deals, then they would be set up on a subsite of the main site (ex: In the MySQL tables, there are two tables for the deals plugin and there are about seven tables for the PayPal pay system (one of which is the affiliate table). These tables are duplicated for EACH of the subsites that are created.

    Using MultiPost-mu, when a deal is put on a page/post in the main site, it is mirrored to any and all subsites that are chosen to receive the mirror of information; however, I actually have to go into Navicat and copy over the two tables for the deals plugin and a couple of the tables for the PayPal payment system or the deal is not complete on the subsites. Ultimately, I want to write either a plugin or just some PHP code with a CRON job to handle this, but initially I will do this manually.

    The buy button is different for each deal, and it can be further modified by the addition of an affiliate in the mix. The idea is that each affiliate would have their own site (either or they can use their own domain and the whole subsite would look like their own branded product with NO reference back to the mother site).

    When there are 10 or 20 affiliates, it would not be overly difficult to go to each database and modify the buy button link to include the affiliate information, but if he grows the site and have 50 or even more affiliates, it would be ludicrous to have to manually modify each of those buy buttons. If I could use a variable so that when someone visits they will get a buy button that credits affiliate "A" and when they visit they will get a buy button that credits affiliate "D", then I would be set. I immediately thought of using a variable for this, but there may be a better way.

    Like I said, I'll study the information posted already and come back with additional questions if I have them.

    Thank you all for your input.


  • James Dunn
    • The Crimson Coder

    Good evening everyone.

    Thanks for the links and the code here. I've studied it all today - while troubleshooting other problems. While the links and code didn't answer this specific situation, the links did lead me to some really good information for another question I was going to be asking soon - interesting how that can happen.

    In case you're curious, I will be needing to set up a CRON job to execute some PHP code that will copy the data in one table to multiple other tables (with the same format), so the code is pretty straightforward. However, the links here took me to the code without me having to research it.

    I'm still trying to figure out how to accomplish this like I want to, but you all have put me much more on the track.

    Thanks again.


Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.