Guest User

Untitled

a guest
Dec 13th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.55 KB | None | 0 0
  1. <?php
  2.  
  3. // Connect to an external database
  4. $db = Craft::createComponent([
  5. 'emulatePrepare' => true,
  6. 'charset' => 'utf8',
  7. 'class' => 'CraftDbConnection',
  8. 'autoConnect' => true,
  9. ]);
  10.  
  11. $db->connectionString = 'mysql:host=localhost;dbname=MY_DB;port=3306';
  12. $db->username = 'UN';
  13. $db->password = 'PW';
  14.  
  15. // Use Query Builder to fetch data from the database
  16. $data = $db->createCommand()
  17. ->select("*")
  18. ->from("TBL")
  19. ->queryAll();
  20.  
  21. // Pass along the data to be used in a template via variables
  22. return $data;
Add Comment
Please, Sign In to add comment