2

I'm trying to convert some PHP code from Mysqli to PDO, and I need to be able to set a default host for PDO MySQL connections (outside of my application code).

With Mysqli, I could do this with the php.ini setting:

mysqli.default_host = ip.of.mysql.server

And then connect with:

$link = mysqli_connect(null,  USERNAME,  PASSWORD, DATABASE);

It's worked beautifully for us.


The equivalent code for PDO:

$link = new PDO('mysql:dbname=' . DATABASE, USERNAME, PASSWORD);

Doesn't work the same way. It works as long as my MySQL server is on localhost. It isn't. I have a separate machine hosting my MySQL server.

Is there an equivalent php.ini setting that could accomplish what I need for PDO? The closest thing I've found is a pdo_mysql.default_socket setting, but I don't think that will do it.

Andrew Ensley
  • 11,611
  • 16
  • 61
  • 73

2 Answers2

2

According to PDO documentation, you can set pdo.dsn aliases in php.ini.

In php.ini:

pdo.dsn.mydb="mysql:dbname=testdb;host=localhost"

In PHP:

$dbh = new PDO('mydb', $user, $password);

Alternatively, you could use a host and change the IP address in the hosts file. That's outside of application code.

Marcus Adams
  • 53,009
  • 9
  • 91
  • 143
1

Socket connections are only useful on local connections only. You must specify the host explicitly

mysql:host=123.123.123.1;dbname=MyDb

Update: Just found something

http://php.net/pdo.configuration

As far as I can see you can define alias for dsns. I didn't try it myself, but it should look like

pdo.dsn.otherServer = mysql:host=example.com;dbname=foo

And then you should be able to use it in PDO::__construct()

$pdo = new PDO('otherServer', $x, $y);

Additional you can give a file path as constructor argument, that points to a file that contains the dsn-string

$pdo = new PDO('uri:file///usr/share/connection.dsn', $x, $y);
// With file /usr/share/connection.dsn that contains: (and nothing more than)
mysql:host=example.com;dbname=foo
KingCrunch
  • 128,817
  • 21
  • 151
  • 173
  • That's what I figured. I know that I can supply the host explicitly. What I'm looking for is a way to set a default host somewhere outside of my application code a la Mysqli. I'm really hoping this is possible. – Andrew Ensley Feb 23 '12 at 18:33
  • Regarding your edit: That's perfect! I'll test that out right now. – Andrew Ensley Feb 23 '12 at 18:41
  • Must say: Didn't realized it before. Sounds quite cool to me too :) That allows to separate the database (configuration) from the application and bind it to the environment directly. – KingCrunch Feb 23 '12 at 18:46
  • Arg! So close, yet so far. It does work, but now my database name is outside the application as well, which right now is dynamically referenced in the code (and needs to be). It doesn't look like I can separate the two. It's either all out or all in. I may have to go the hosts file route. – Andrew Ensley Feb 23 '12 at 18:52
  • How dynamic should the database name be? You can define as many alias as you like. If you have 5 databases, why you just don't define 5 alias? Or maybe have a look at the second solution: Write your several databases into separate files and load them via the `uri:`-style dsn. – KingCrunch Feb 23 '12 at 18:54
  • The problem is that the application uses the same code on multiple servers for multiple "insallations" which are designated by sub-domain. The sub-domain corresponds to the database name, the user, and a single config file with the database password. With the alias scenario, I would have to put a new line in PHP.ini for each installation (and on each server) in addition to the config file. With the file route, I'd then need two config files, one of which could not be synchronized between servers because its contents would have to be different on each. The Mysqli setup was/is perfect for us. – Andrew Ensley Feb 23 '12 at 19:03