1
0
mirror of synced 2024-12-15 15:46:02 +03:00
doctrine2/manual/docs/Basic Components - Db - Connecting to a database.php

57 lines
1.4 KiB
PHP
Raw Normal View History

2006-11-11 00:29:13 +03:00
<?php ?>
Doctrine_Db allows both PEAR-like DSN (data source name) as well as PDO like DSN as constructor parameters.
<br \><br \>
Getting an instance of Doctrine_Db using PEAR-like DSN:
<br \><br \>
<?php
2006-11-11 00:29:13 +03:00
$str = "<?php
// using PEAR like dsn for connecting pgsql database
2006-11-11 00:29:13 +03:00
\$dbh = new Doctrine_Db('pgsql://root:password@localhost/mydb');
2006-11-11 00:29:13 +03:00
// using PEAR like dsn for connecting mysql database
2006-10-19 23:22:10 +04:00
2006-11-11 00:29:13 +03:00
\$dbh = new Doctrine_Db('mysql://root:password@localhost/test');
?>";
renderCode($str);
?>
<br \><br \>
Getting an instance of Doctrine_Db using PDO-like DSN (PDO mysql driver):
<br \><br \>
<?php
$str = "<?php
\$dbh = new Doctrine_Db('mysql:host=localhost;dbname=test',
\$user, \$pass);
?>";
renderCode($str);
?>
<br \><br \>
Getting an instance of Doctrine_Db using PDO-like DSN (PDO sqlite with memory tables):
<br \> <br \>
<?php
$str = "<?php
\$dbh = new Doctrine_Db('sqlite::memory:');
?>";
renderCode($str);
?>
<br \><br \>
2006-11-11 00:29:13 +03:00
Handling connection errors:
2006-11-11 00:29:13 +03:00
<?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() . '<br />';
die();
}
?>";
renderCode($str);
?>