I need to retrieve data from at least 3 databases, is there something wrong with reusing my PDO objects?
$dbh = new PDO('mysql:host=' . $host . ';dbname=' . $db_name, $user, $password);
$sth = $dbh->prepare($query1);
$dbh = new PDO('mysql:host=' . $host2 . ';dbname=' . $db_name2, $user2, $password2);
$sth = $dbh->prepare($query2);
Sorry for editing, but here's another consideration. With each of them, I obviously have to check if the connection was successful, and throws an exception if this is not the case:
if (!$dbh) {
$err=$dbh->errorInfo();
throw new Exception('Could not connect: ' . $err[2]);
}
I do not think that there is a way to avoid this if I do not create all the connections at the same time and do not if (!dbh1|!dbh2) { ... }. Just think something else.
source
share