为什么MySQL和PostgreSQL的PHP​​ PDO DSN格式不同? [英] Why is PHP PDO DSN a different format for MySQL versus PostgreSQL?

查看:50
本文介绍了为什么MySQL和PostgreSQL的PHP​​ PDO DSN格式不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用PDO连接到MySQL数据库时,我需要连接的方式是:

When I connect to a MySQL database using PDO, the way I need to connect is:

$pdoConnection = new PDO("mysql:host=hostname;dbname=databasename",user,password);

但是,对于PostgreSQL来说,DSN是更标准的(IMO):

But, for PostgreSQL, the DSN is more standard (IMO):

$pdoConnection = new PDO("pgsql:host=hostname;dbname=databasename;user=username;password=thepassword");

MySQL是否有任何原因不能使用单个字符串?还是因为我正在使用的版本(PHP 5.2,MySQL 5.0,PostgreSQL 8.1)?

Is there any reason why MySQL cannot use a single string? Or is this just because of the versions I am using (PHP 5.2, MySQL 5.0, PostgreSQL 8.1)?

推荐答案

作为同时实现这两者的人,我可以告诉你,原因是通过将字符串按原样传递给Postgres(和ODBC)PDO驱动程序这些数据库的代码不需要更新,因为基础库添加了新功能.

As the person that implemented both, I can tell you that the reason is that by passing the string through as-is to postgres (and ODBC) the PDO driver code for those databases does not need to be updated as the underlying library adds new features.

由于MySQL没有自己的连接字符串解析代码,因此我们发明了一种将数据传递到底层MySQL函数调用的机制,该函数具有非常固定的参数API.

Since MySQL does not have its own connection string parsing code, we invented a mechanism for passing data in to the underlying MySQL function calls, which have a very specific API with fixed parameters.

没有意外;这是非常有意的.

No accident; it's very deliberate.

这篇关于为什么MySQL和PostgreSQL的PHP​​ PDO DSN格式不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆