1
0
mirror of synced 2024-12-15 07:36:03 +03:00
doctrine2/manual/docs/Working with objects - Component overview - Db - Connecting to a database.php
legenerationlazi 13840516a9 removed empty php tags at the top of some pages
html->wiki conversion previously missed
2007-04-19 20:41:19 +00:00

74 lines
1.2 KiB
PHP

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);
?>