Once again, you do NOT pass the handle around between client and server. If the client does need to specify which database connection to use, but otherwise uses the exact same tables and queries, you could do it like this (which is according to the previous post), and in my opinion only suitable if you for some reason connect to several databases on every request
$db1 = new PDO(...);
$db2 = new PDO(...);
if (!empty($_POST)) {
# handle is not specified. use default
if (!isset($_POST['handle'])) {
$handle = $db1;
}
# specified handle does not exist. use default. (or perhaps log error and exit)
# e.g. $_POST['handle'] == 'db3' => $db3 is not set...
elseif (!isset($$_POST['handle']) {
$handle = $db1;
}
else {
# if $_POST['handle'] == 'db1', then $handle = $db1;
# if $_POST['handle'] == 'db2', then $handle = $db2;
$handle = $$_POST['handle'];
}
$stmt = $handle->query(...);
}
Otherwise you might just as well do it like this
$db1 = new PDO(...);
$db2 = new PDO(...);
if (!empty($_POST)) {
# handle is not specified. use default
if (!isset($_POST['handle'])) {
$handle = $db1;
}
# specified handle does not exist. use default. (or perhaps log error and exit)
# e.g. $_POST['handle'] == 'db3' => $db3 is not set...
elseif (!isset($$_POST['handle']) {
$handle = $db1;
}
else {
# if $_POST['handle'] == 'db1', then $handle = $db1;
# if $_POST['handle'] == 'db2', then $handle = $db2;
$handle = $$_POST['handle'];
}
$stmt = $handle->query(...);
}
It