<p>If only the service will be connecting to it and only one method, then your example works fine.</p>

<p>If only the service will be connecting to, but you use it across multiple methods, then you might want to move the bootstrapping part to the service <code>init()</code> method and save the database connection as a private instance variable in the service so everything in the service can access it.</p>

<blockquote>
  <p>Do I need to manually "close" this connection</p>
</blockquote>

<p>I wouldn't worry about it.</p>

CC BY-SA 4.0
2018-12-15T01:48:57.860Z
2018-12-15T01:48:57.860Z
Brad Bell
<p>I recently saw a very helpful post about how to connect to an external database from a Craft plugin and I wanted to confirm I understood the implementation correctly.</p>

<p>On <a href="https://craftcms.stackexchange.com/a/8977/712">this response</a> the author recommended a way to leverage Yii's Query Builder on external databases. Is the following snippet the way to properly implement this in a service? Do I need to manually "close" this connection or is this alone the way to implement this?</p>

<pre><code>&lt;?php

// Connect to an external database
$db = Craft::createComponent([
    'emulatePrepare'    =&gt; true,
    'charset'           =&gt; 'utf8',
    'class'             =&gt; 'Craft\DbConnection',
    'autoConnect'       =&gt; true,
]);

$db-&gt;connectionString = 'mysql:host=localhost;dbname=MY_DB;port=3306';
$db-&gt;username = 'UN';
$db-&gt;password = 'PW';

// Use Query Builder to fetch data from the database
$data = $db-&gt;createCommand()
    -&gt;select("*")
    -&gt;from("TBL")
    -&gt;queryAll();

// Pass along the data to be used in a template via variables
return $data;
</code></pre>

CC BY-SA 4.0
2018-12-15T01:58:50.127Z
You can see how the [Connect](https://github.com/nystudio107/craft-connect) plugin does it, if that helps ya.
andrew.welch
<plugin-development><database><query><yii>
Connect to an external database from a Craft 2.x plugin
# Connect to an external database from a Craft 2.x plugin

(Asked by: username_0 on 2018-12-14)

I recently saw a very helpful post about how to connect to an external database from a Craft plugin and I wanted to confirm I understood the implementation correctly.
On this response the author recommended a way to leverage Yii's Query Builder on external databases. Is the following snippet the way to properly implement this in a service? Do I need to manually "close" this connection or is this alone the way to implement this?
 true,
    'charset'           => 'utf8',
    'class'             => 'Craft\DbConnection',
    'autoConnect'       => true,
]);

$db->connectionString = 'mysql:host=localhost;dbname=MY_DB;port=3306';
$db->username = 'UN';
$db->password = 'PW';

// Use Query Builder to fetch data from the database
$data = $db->createCommand()
    ->select("*")
    ->from("TBL")
    ->queryAll();

// Pass along the data to be used in a template via variables
return $data;

**Tags:** plugin-development, database, query, yii
> **Comments:**
> **username_1**: You can see how the [Connect](https://github.com/nystudio107/craft-connect) plugin does it, if that helps ya.  

---

## 1 answers

### Answer by username_2 on 2018-12-15. Score: 1 | Accepted answer

If only the service will be connecting to it and only one method, then your example works fine.

If only the service will be connecting to, but you use it across multiple methods, then you might want to move the bootstrapping part to the service `init()` method and save the database connection as a private instance variable in the service so everything in the service can access it.

> Do I need to manually "close" this connection

I wouldn't worry about it.


---

