Skip to content

Instantly share code, notes, and snippets.

@racmanuel
Forked from rvgpl/Query external database
Created March 15, 2022 20:40
Show Gist options
  • Save racmanuel/e71e76285279f9cb46732ca87d6ec777 to your computer and use it in GitHub Desktop.
Save racmanuel/e71e76285279f9cb46732ca87d6ec777 to your computer and use it in GitHub Desktop.
Query external database from Wordpress
###################################################################################################
Query regular Wordpress database
###################################################################################################
//Declaring $wpdb as global and using it to execute an SQL query statement that returns a PHP object
global $wpdb;
$result = $wpdp->get_results('SELECT * FROM wp_options, OBJECT);
########################################################################################################
Querying a different database
########################################################################################################
//Create a new wpdb variable with other database connect values and use it to execute SQL query statement that return a PHP Object
$newdb = new wpdb($DB_USER, $DB_PASSWORD, $DB_NAME, $DB_HOST);
$newdb->get_results('SELECT * FROM users, OBJECT');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment