2
Hello, I connect in a bank , how would I connect with another, which has the same login and password within the same server ? follows my function:
function conectaBanco() {
$hostname='dbprovider.rede.local';
$username='root';
$password='8asd331221!';
try {
$dbh = new PDO("mysql:host=$hostname;dbname=Provider",$username,$password,
array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8'"));
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return $dbh;
}
catch(PDOException $e) {
echo $e->getMessage();
}
}
in the case as I do here to connect with the other bank, example I want to put
$dbh2 = new PDO("mysql:host=$hostname;dbname=Provider2",$username,$password,
but it didn’t work !!! anyone can help ? the error is Uncaught Error: Call to a Member Function query() on null line 48 and that line has
$dbh = conectaBanco();
Why "didn’t it work"? Did it give an error? Which one? By the way, it seems to be the same server, only Databases different, so why, instead of creating two connections, does not use the same with the
use Provider2
?– Woss
only creates separate functions one for each bank
– 13dev
I would create a function with a property and an internal switch or if, so standardizes and facilitates. Example:
connDB('BD1'), connDB('BD2'), connDB('BD3')
, etc Now the reason for the error, can be instance, user permission, wrong BD name, etc... If you do not put the error, can not guess.– rbz
@Very good idea, I think his mistake is that it is instantiating two connections and returning only one
– 13dev
the error is Uncaught Error: Call to a Member Function query() on null line 48 and on that line you have $dbh = connect();
– J Junior Carneiro
I have to change everything to $dbh2 ?
– J Junior Carneiro
attaches the error to the question and the code in question
– 13dev
@Rbz I will follow this function pattern for connection from now on, before I made one for each bank, great idea
– Pedro Augusto
@Rbz can give an example of what this function would look like ?
– J Junior Carneiro
Posted. But remember, it’s basic, upon your code.
– rbz