Last active
August 29, 2015 14:10
-
-
Save mattsandersuk/5235f72f0a62006322f2 to your computer and use it in GitHub Desktop.
Quick and dirty way to connect and display data from external DB in WordPress
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* | |
* Define DB | |
* | |
**/ | |
// input credentials | |
$mydb = new wpdb('root','mysql','TABLENAME','localhost'); | |
// select all columns | |
$rows = $mydb->get_results("SELECT * | |
FROM example_table_1 a | |
-- specify dominant table (and assign 'a') | |
LEFT JOIN example_table_2 b | |
-- specify joined table (and assign 'b') | |
ON a.unique_id = b.another_id | |
ORDER BY a_field_name | |
"); | |
/** | |
* | |
* Foreach | |
* | |
**/ | |
foreach ( $rows as $obj ){ | |
echo $obj->a_field_name; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment