Skip to content

Instantly share code, notes, and snippets.

@mattsandersuk
Last active August 29, 2015 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattsandersuk/5235f72f0a62006322f2 to your computer and use it in GitHub Desktop.
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
<?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