Two different wordpress sites – same server and IP address. Gaining Access to database 1 of 2

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.