1
0
mirror of synced 2024-12-14 23:26:04 +03:00
doctrine2/manual/docs/Working with objects - Component overview - Db - Connecting to a database.php

74 lines
1.2 KiB
PHP
Raw Normal View History

2007-04-14 01:49:11 +04:00
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);
?>