How do I PDO connect to multiple Databases?

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

How do I PDO connect to multiple Databases?

Post 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.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: How do I PDO connect to multiple Databases?

Post 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...
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do I PDO connect to multiple Databases?

Post by simonmlewis »

It is the name of a database connection.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: How do I PDO connect to multiple Databases?

Post 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?
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do I PDO connect to multiple Databases?

Post 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');
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How do I PDO connect to multiple Databases?

Post 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.
Post Reply