Connect to another database in WordPress using the wpdb class

In WordPress the $wpdb object can talk to any number of tables, but only within database WordPress is installed. In case you need to connect to another database, you will have to instantiate your own object from the wpdb class with the appropriate connection details:

$mydb = new wpdb( 'username' , 'password' , 'database' , 'localhost' );

 

Once the connection is made you can access the database and its tables as you would in any WordPress database:

$rows = $mydb->get_results("SELECT field1,field2 FROM myTable WHERE field3=1");
foreach ($rows as $row) {
  echo $row->field1;
}

This code can be in plugins, templates, themes and almost anywhere else inside the WordPress code.

WordPress Roles and Capabilities – list

WordPress contains the roles Administrator, Editor, Author and Contributor which gives the following capabilities:

Administrator
■ activate_plugins
■ add_users
■ create_users
■ delete_others_pages
■ delete_others_posts
■ delete_pages
■ delete_plugins
■ delete_posts
■ delete_private_pages
■ delete_private_posts
■ delete_published_pages
■ delete_published_posts
■ delete_themes
■ delete_users
■ edit_dashboard
■ edit_files
■ edit_others_pages
■ edit_others_posts
■ edit_pages
■ edit_plugins
■ edit_posts
■ edit_private_pages
■ edit_private_posts
■ edit_published_pages
■ edit_published_posts
■ edit_theme_options
■ edit_themes
■ edit_users
■ export
■ import
■ install_plugins
■ install_themes
■ list_users
■ manage_categories
■ manage_links
■ manage_options
■ moderate_comments
■ promote_users
■ publish_pages
■ publish_posts
■ read_private_pages
■ read_private_posts
■ read
■ remove_users
■ switch_themes
■ unfiltered_html
■ unfiltered_upload
■ update_core
■ update_plugins
■ update_themes
■ upload_files

Editor
■ delete_others_pages
■ delete_others_posts
■ delete_pages
■ delete_posts
■ delete_private_pages
■ delete_private_posts
■ delete_published_pages
■ delete_published_posts
■ edit_others_pages
■ edit_others_posts
■ edit_pages
■ edit_posts
■ edit_private_pages
■ edit_private_posts
■ edit_published_pages
■ edit_published_posts
■ manage_categories
■ manage_links
■ moderate_comments
■ publish_pages
■ publish_posts
■ read
■ read_private_pages
■ read_private_posts
■ unfiltered_html
■ upload_files

Author
■ delete_posts
■ delete_published_posts
■ edit_posts
■ edit_published_posts
■ publish_posts
■ read
■ upload_files

Contributor
■ delete_posts
■ edit_posts
■ read

For more further details see WordPress Codex here.