Page 1 of 1
How do I PDO connect to multiple Databases?
Posted: Mon Oct 12, 2015 4:55 am
by simonmlewis
Code: Select all
$query = ("SELECT * FROM members WHERE usertype <> 'admin'", $dbmembers);
$result = $pdo->query($query);
while ($row = $result->fetch(PDO::FETCH_OBJ))
{
echo "<option value='$row->id'>$row->firstname $row->lastname</option>";
}
This is causing errors with the comma before $dbmembers.
We are doing this because it's running from a separate web site, so need to connect it.
If I do this without using 'pdo', it works.
Re: How do I PDO connect to multiple Databases?
Posted: Mon Oct 12, 2015 6:14 am
by requinix
What is $dbmembers supposed to be? It's... it's not a database name, right? That's all I can think of, but...
Re: How do I PDO connect to multiple Databases?
Posted: Mon Oct 12, 2015 6:32 am
by simonmlewis
It is the name of a database connection.
Re: How do I PDO connect to multiple Databases?
Posted: Mon Oct 12, 2015 6:56 am
by requinix
Database connections don't really have names in code... Do you mean that it's a PDO object? If so then what is $pdo? Or put another way, how is $dbmembers initialized?
Re: How do I PDO connect to multiple Databases?
Posted: Mon Oct 12, 2015 7:14 am
by simonmlewis
Code: Select all
$dbmembers = mysql_connect("localhost","root","")or die ('Could not connect to using those details');
mysql_select_db('members', $dbmembers) or die ('Could not connect to members Database');
Re: How do I PDO connect to multiple Databases?
Posted: Mon Oct 12, 2015 8:04 am
by Celauran
You're trying to mix and match PDO objects with resources from mysql_connect. That's not going to work. You would need a separate PDO connection for the remote database and then execute the query against that.
Code: Select all
$pdo_members = new PDO('credentials go here');
$query = ("SELECT * FROM members WHERE usertype <> 'admin'");
$result = $pdo_members->query($query);
while ($row = $result->fetch(PDO::FETCH_OBJ))
{
echo "<option value='$row->id'>$row->firstname $row->lastname</option>";
}
Or something to that effect.