How to Extend WP Plugin MySQL connection array to Multiple??

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
Maverick
New php-forum User
New php-forum User
Posts: 1
Joined: Fri Mar 10, 2017 6:44 am

Fri Mar 10, 2017 7:49 am

In a plugin for WP that I bought I can make table views from tables As LONG as they are in the same Database as the WordPress Database installation tables.
The default WP config database connection is where the plugin looks when MySQL queries are entered.
But I need to access OTHER databases in my MySQL account at the same localhost as my WordPress installation tables.

The plugin says I can specify an alternative connection with the standard Host, DB Name, User name, password, port parameters.
So far this alternate method when the test script is run before trying to save the new connection cannot find any other database but the one in the base config.inc.php file.

The new Alternate Connection script is saved in a Tab Admin view in the plugin called "Settings"

-----
There is a plugin function for Defining the Alternate connection settings that looks like this:

Code: Select all

/**
 * MySQL settings for query-based tables
 */ 
define( 'WDT_ENABLE_MYSQL', true ); // Whether to use MySQL in DataTables. Disable if you are not going to access MySQL directly from DataTables.
if(get_option('wdtUseSeparateCon'))
{
    define( 'WDT_MYSQL_HOST', get_option('wdtMySqlHost') ); // Name or address of MySQL host
    define( 'WDT_MYSQL_DB', get_option('wdtMySqlDB') ); // Name of MySQL database to use
    define( 'WDT_MYSQL_USER', get_option('wdtMySqlUser') ); // Name of MySQL user
    define( 'WDT_MYSQL_PASSWORD', get_option('wdtMySqlPwd') ); // Password to use in MySQL
    define( 'WDT_MYSQL_PORT', get_option('wdtMySqlPort') ); // Password to use in MySQL
}
is this a foreach($data as $array type of solution?

I am confused about the different ways this script can be modified and I know they are not all equal --
So, my basic need is this:

I want to extend this array group with another Loop so that the function can save many array configurations using the same defined names
but each array grouped together so that it could be referenced by the get_option('wdtMySqlDB') variable

so that available in the WP Admin plugin Menu 'Settings' Admin Tab

I can use a form selector to Choose a grouped array named 'dbpharmacies' to use with a new table view I want to create or view
The dropdown list that I will modify then can show another array called 'dbautomobiles' for connecting to that database


Code: Select all

define('New_Connection_dbpharmacies');
AS
{
    define( 'WDT_MYSQL_HOST', get_option('wdtMySqlHost') ); // Name or address of MySQL host
    define( 'WDT_MYSQL_DB', get_option('wdtMySqlDB') ); // Name of MySQL database to use
    define( 'WDT_MYSQL_USER', get_option('wdtMySqlUser') ); // Name of MySQL user
    define( 'WDT_MYSQL_PASSWORD', get_option('wdtMySqlPwd') ); // Password to use in MySQL
    define( 'WDT_MYSQL_PORT', get_option('wdtMySqlPort') ); // Password to use in MySQL
}
I know that this array group is referred in the

Code: Select all

[i]referred to in ajax actions file
in a separate ajax_actions.php[/i]

            // If plugin is using a separate DB
            $sql = new PDTSql(WDT_MYSQL_HOST, WDT_MYSQL_DB, WDT_MYSQL_USER, WDT_MYSQL_PASSWORD, WDT_MYSQL_PORT);
and in another admin script

Code: Select all

            $mysql_connection_settings = $_POST['mysql_settings'];
            
            try{
                $Sql = new PDTSql(  
                            $mysql_connection_settings['host'],
                            $mysql_connection_settings['db'],
                            $mysql_connection_settings['user'],
                            $mysql_connection_settings['password'],
                            $mysql_connection_settings['port']
                        );
                if( $Sql->isConnected() ){
                    $return_array['success'][] = __( 'Successfully connected to the MySQL server', 'datatables' );
                }else{
                    $return_array['errors'][] = __( 'DataTables could not connect to MySQL server.', 'datatables' );
                }
            }catch( Exception $e ){
                $return_array['errors'][] = __( 'DataTables could not connect to MySQL server. MySQL said: ', 'datatables' ) . $e->getMessage();
            }
            echo json_encode( $return_array );
            exit();
        }

NigelRen
php-forum Fan User
php-forum Fan User
Posts: 622
Joined: Fri Aug 05, 2011 9:53 am

Fri Mar 10, 2017 9:37 am

I really don't know how this can be done as your talking about - but there is an alternative... using federated tables. This means that in the default database, you can link to tables on other databases.
Have a look at https://dev.mysql.com/doc/refman/5.5/en ... ction.html which explains how they can be set up.

Post Reply