0

Bit of an unusual request. I have a series of individual wordpress websites. they are not a multisite, as I need each to have unique domain names for the various sections of network.

What I want to do, is be able to access a unique plugin table that resides on one of the sites, but not the other for data display purposes. The reason I do this is, all the payments for this subscription service go through one site, but not the other. Saves money instead of having to purchase 3 or more version of woocommerce subscriptions.

So far I have tried :

 define('DB_SERVER', 'xxx.xxx.xxx.xxx');
 define('DB_USERNAME', 'sister_site');
 define('DB_PASSWORD', 'xxxxxx');
 define('DB_DATABASE', 'database1_wrdp1');  
 $db = mysqli_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);
 if (!$db)
   {
   DebugLog("Error: Unable to connect to MySQL."  );
   DebugLog("Debugging errno: " . mysqli_connect_errno());
   DebugLog("Debugging error: " . mysqli_connect_error());
   }
else
  DebugLog('Connection SUCCESS!' );

I get the error: Error: Unable to connect to MySQL. Access denied for user xxxxx

I have assigned a unique user for the database with the information and inserted that into the othersites PHP code and am attempting to just connect. No luck.

Anyone have any ideas on how the one site can gain access to the wordpress database in the other site. All sites are on the same IP address, same server, just independent accounts.

4
  • Not sure if this typo is also in your actual code, but in the first line you're missing the closing single quote ' after the db server IP address.
    – WebElaine
    Commented Jun 27, 2019 at 20:06
  • Thanks...that is a typo from copying it from the code. The code has the close quote. Commented Jun 27, 2019 at 20:08
  • Your host might be able to help. Your code looks like it should work, assuming the right server, database, and credentials are filled in.
    – WebElaine
    Commented Jun 27, 2019 at 20:10
  • I own the server...so, I am "the host". Standard Linix . I have done this before, but not in a wordpress environment. Thus the question. Commented Jun 27, 2019 at 20:12

1 Answer 1

1

Got it...took all day, but now finally I can finally sleep.

This solution is for a set of databases that reside on the same server, even if they are in different accounts. Thus you do not need to set the remote server access in the CPanel. The key is to find the user name and password, for the wordpress database you wish to gain access to. This can be done in the wp_config.php OR assign a new user to the database in the Cpanel.

First problem, I experienced was #define statements need to be in double quotes, not single.

To access the database using the method I originally tried , (again from the same server), even if it is a different account would be as follows:

 $db = mysqli_connect("localhost","user name","password","database name");
 if (!$db)
   {
    DebugLog("Error: Unable to connect to MySQL."  );
    DebugLog("Debugging errno: " . mysqli_connect_errno());
    DebugLog("Debugging error: " . mysqli_connect_error());
   }
 else
  {
  DebugLog('3 Connection SUCCESS!' );
  mysqli_close($db);
 }

But this did not allow the accessing of WordPress databases in the traditional way. Since the goal was to have one wordpress site, access the data from another wordpress site, another method was needed AND to be able to go back and forth with the same function calls, this required accessing of the data via the WordPress way. This is that solution:

  function connectAltDB()
   {
    global $new_wpdb;
    //                   "user","password", "database", "host/ip"
    $new_wpdb = new wpdb("wrdp1_user", "YourPass", "wrdp1_db", "localhost");
    $new_wpdb->set_prefix('wp_', $set_table_names = true);
    $new_wpdb->show_errors();

   // Test the connection with a simple retrieval - can be commented out
   //$sql_query = $new_wpdb->prepare("SELECT * FROM wp_users WHERE id=%d", 1);
  // $rows      = $new_wpdb->get_row($sql_query, ARRAY_A);
  // DebugLog($rows['user_login']);

  return $new_wpdb;
  }

 function CloseconnectAltDB()
  {
  global $new_wpdb;
  $new_wpdb->close()  ; 
  }

Hopes this helps someone in the future.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.