Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Just add another dsn for your other DB, and connect to that one using PDO...</p> <p>As a matter of fact, we defined our databases in the Zend config as follows (using XML), to cater for multiple DB connections :</p> <pre><code> &lt;databases&gt; &lt;db_one&gt; &lt;adapter&gt;pdo_mysql&lt;/adapter&gt; &lt;params&gt; &lt;dbname&gt;...&lt;/dbname&gt; &lt;username&gt;...&lt;/username&gt; &lt;password&gt;...&lt;/password&gt; &lt;host&gt;...&lt;/host&gt; &lt;port&gt;...&lt;/port&gt; &lt;/params&gt; &lt;/db_one&gt; &lt;db_two&gt; &lt;adapter&gt;pdo_mysql&lt;/adapter&gt; &lt;params&gt; &lt;dbname&gt;...&lt;/dbname&gt; &lt;username&gt;...&lt;/username&gt; &lt;password&gt;...&lt;/password&gt; &lt;host&gt;...&lt;/host&gt; &lt;port&gt;...&lt;/port&gt; &lt;/params&gt; &lt;/db_two&gt; &lt;/databases&gt; </code></pre> <p>(Of course they're not really called db_one and db_two, but have a proper name :p).</p> <p><strong>edit</strong></p> <p>You could initialize the DB connections as follows (call this somewhere in your bootstrap) :</p> <pre><code>private function initDb() { foreach ($this-&gt;config-&gt;databases as $name =&gt; $database) { try { $db = Zend_Db::factory($database); // Hack for MySQL utf8 encoding... if ($database-&gt;adapter == 'pdo_mysql') { $db-&gt;getConnection(); $db-&gt;query('SET NAMES utf8'); } Zend_Registry::set($name, $db); } catch (Zend_Db_Adapter_Exception $e) { throw new Application_Exception($e-&gt;getMessage()); } catch (Zend_Exception $e) { throw new Application_Exception($e-&gt;getMessage()); } } } </code></pre> <p>Then, if you want to perform a query on db_two anywhere in your code, you can use :</p> <pre><code>$db = Zend_Registry::get('db_two'); $stmt = $db-&gt;query('select ... from ...'); </code></pre> <p>And use fetch() or fetchAll() on $stmt as you see fit.</p> <p>BTW You don't have to use Zend_Registry and open all connections on every request of course, so just consider this as an example implementation, not as a guideline on how to solve your problem.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload