I had a problem, which I solved but I don't fully understand why the solution works:
Without the '$conn = null;' statement in the code below the number of database connections was increasing and I got a "too many database connections" error. With that statement it works fine - no increasing nr of dbase connections, but I would have assumed that the '$conn = new Database($dbname[$day]);' statement would set a new database connection, and therefore automatically close the previous one. Obvious not, but why?
for($day=0;$day<365;$day++){
//code to determine $dbname here...
$conn = new Database($dbname[$day]); //Database() is defined class
//code to access database $dbname
$conn = null; //close connection
}//for
UPDATE: the the constructor for the Database class as requested:
public function __construct($dbname) {
try {
if($dbname != ECIS_DB_NAME){ //access to master database is forbidden
$this->_db = new PDO('mysql:host='.ECIS_DB_HOST.';dbname='.$dbname, ECIS_DB_USER, ECIS_DB_PASS, array(
PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true
));
$this->_db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->_db->query('SET CHARACTER SET utf8');
self::$count++;
}//if
} catch (PDOException $e) {
exit('Error while connecting to database.'.$e->getMessage());
}
}//public function __construct