使用 PHP PDO 设置 PostgreSQL 模式的最佳替代方案 [英] Best alternative to set a PostgreSQL schema using PHP PDO

查看:45
本文介绍了使用 PHP PDO 设置 PostgreSQL 模式的最佳替代方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 PHP PDO 访问具有各种架构的 PostgreSQL 数据库,因此首先我创建了一个连接,然后设置了正确的架构,如下所示:

I am using PHP PDO to access a PostgreSQL database with various schemas, so first i create a connection and then i set the correct schema, like below:

$Conn = new PDO('pgsql:host=localhost;port=5432;dbname=db', 'user', 'pass');

$result = $Conn->exec('SET search_path TO accountschema');

if ( ! $result) {
    die('Failed to set schema: ' . $Conn->errorMsg());
}

这是一个好习惯吗?有没有更好的方法来做到这一点?

Is this a good practice? Is there a better way to do this?

推荐答案

为了指定默认架构,您应该设置 search_path 代替.

In order to specify the default schema you should set the search_path instead.

$Conn->exec('SET search_path TO accountschema');

您还可以为每个数据库用户设置默认的 search_path,在这种情况下,上述语句变得多余.

You can also set the default search_path per database user and in that case the above statement becomes redundant.

ALTER USER user SET search_path TO accountschema;

这篇关于使用 PHP PDO 设置 PostgreSQL 模式的最佳替代方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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