I've just started using PDO and was wondering how best to declare the database connection?
Would it best practice to create a script as follows, called config.php for example
config.php
<?php
$dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass, array(
PDO::ATTR_PERSISTENT => true
));
?>
Then have example.class.php
<?php
include config.php;
class Example {
public function fetch() {
$data = $dbh->query('SELECT * FROM myTable WHERE name = ' . $conn->quote($name));
// do stuff
}
}
?>
And do this for all my classes? Or would this make multiple connections? I want to have as few connections as possible.