Doctrine_Db allows both PEAR-like DSN (data source name) as well as PDO like DSN as constructor parameters.



Getting an instance of Doctrine_Db using PEAR-like DSN:



<?php
$str = "<?php
// using PEAR like dsn for connecting pgsql database

\$dbh = new Doctrine_Db('pgsql://root:password@localhost/mydb');

// using PEAR like dsn for connecting mysql database

\$dbh = new Doctrine_Db('mysql://root:password@localhost/test');
?>";
renderCode($str);
?>



Getting an instance of Doctrine_Db using PDO-like DSN (PDO mysql driver):



<?php
$str = "<?php
\$dbh = new Doctrine_Db('mysql:host=localhost;dbname=test', 
                        \$user, \$pass);
?>";
renderCode($str);
?>



Getting an instance of Doctrine_Db using PDO-like DSN (PDO sqlite with memory tables):

 

<?php
$str = "<?php
\$dbh = new Doctrine_Db('sqlite::memory:');
?>";
renderCode($str);
?>




Handling connection errors:

<?php
$str = "<?php
try {
   \$dbh = new Doctrine_Db('mysql:host=localhost;dbname=test', 
                           \$user, \$pass);
   foreach (\$dbh->query('SELECT * FROM foo') as \$row) {
     print_r(\$row);
   }
   \$dbh = null;
} catch (PDOException \$e) {
   print 'Error!: ' . \$e->getMessage() . '
';
   die();
}
?>";
renderCode($str);
?>