且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用PDO PHP在Postgresql DB中设置与模式的连接

更新时间:2022-11-05 16:58:57

要设置默认架构,您必须执行以下查询:

$conn = Foo::Conecction();
$conn->exec('SET search_path TO yourschema');

或者,如果您想以更特定于用户的方式进行操作:

$conn->exec('ALTER USER user SET search_path TO yourschema');

作为旁注:请不要创建您的PDO实例(作为静态方法的返回值). PDO直接提供了一个干净的API.您不允许调用者确定要连接的数据库,而是对凭据进行硬编码.任何标准都认为这是不良做法.考虑将连接传递到您需要的任何地方,或者-如果您坚持要创建-创建至少要求调用者自己传递数据库凭据的方法.

我还建议您在您的DSN字符串中包含排序规则,并可能设置一些属性以使调试时的工作更轻松:

$pdo = new PDO(
    "pgsql:dbname=$dbname;host=$host;port=5432;charset=utf8",
    $user,
    $pass,
    [
        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,//set PDO to throw exceptions on error
        PDO::ATTR_ORACLE_NULLS => PDO::NULL_NATURAL,//NULL values are returned as PHP null's
    ]
);

查看其他属性并相应地使用.

最后一点: :前段时间PDO有时可能在解析localhost主机名时遇到麻烦.我怀疑此错误已得到修复,但以防万一它出现了(甚至:尚未修复),我建议您尽可能使用ip地址.如果您已经知道 IP,那么不必麻烦DNS服务器来解析一个您知道该字符串仍会解析为127.0.0.1的字符串.

If I have a Postgresql DB with several Schemas and I want to set a connection with my Web App to one of this shemas (not public schema by default) of the DB using PDO, what will be the most optimal way?

The connection with the DB I do currently like this:

public static function Conecction(){

        $dbname = "start";
        $host = "localhost";
        $username = "user";
        $password = "123";
        return new PDO("pgsql:dbname=$dbname;host=$host", $username, $password);
    }

But here I just specified the name of the DB not with schema inside of the DB

Thx very much!

To set the default schema, you'll have to execute a query like:

$conn = Foo::Conecction();
$conn->exec('SET search_path TO yourschema');

or, if you want to go about it in a more user-specific way:

$conn->exec('ALTER USER user SET search_path TO yourschema');

As a side-note: Please don't create your PDO instances like that (as the return value of a static method). PDO offers a clean API right out of the box. You're not allowing the caller to determine what DB to connect to, instead, you're hard coding the credentials. This is considered bad practice by any standard. Consider passing the connection to wherever you need it, or -if you insist- create a method that at least requires the caller to pass DB credentials themselves.

I'd also recommend you include the collation in your DSN string, and possibly set some attributes to make life easier when debugging:

$pdo = new PDO(
    "pgsql:dbname=$dbname;host=$host;port=5432;charset=utf8",
    $user,
    $pass,
    [
        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,//set PDO to throw exceptions on error
        PDO::ATTR_ORACLE_NULLS => PDO::NULL_NATURAL,//NULL values are returned as PHP null's
    ]
);

Check out other attributes and use accordingly.

Last thing: Some time ago PDO could occasionally run into trouble resolving the localhost host-name. I suspect this bug has been fixed already, but just in case it crops up (or even: hasn't been fixed), I'd recommend you use ip addresses whenever you can. If you know the IP already, there's no real point in having to bother the DNS server with resolving a string that you know will resolve to 127.0.0.1 anyway.