【问题标题】:Access a specific schema in a PostgreSQL database via PHP通过 PHP 访问 PostgreSQL 数据库中的特定模式
【发布时间】:2013-04-13 03:17:37
【问题描述】:

我有一个 PostgreSQL 数据库 userdb 有 5 个模式。

Schema 1- Persons
Schema 2- Project
Schema 3- Shop
Schema 4- Test

我能够使用pg_connect 连接到数据库。如何访问该数据库中的特定架构?

当数据库中只有一个模式时,我能够连接到模式。但现在由于我有多个架构,我很难访问任何特定的架构。

 <?php
 // attempt a connection
 $dbh = pg_connect("host=**.****.*******.*** dbname=test user=merlin port=5433 password=passw123");
 if (!$dbh) {
     die("Error in connection test: " . pg_last_error());
 } 
// execute query
 $sql = "SELECT * FROM test.country";
 $result = pg_query($dbh, $sql);
 if (!$result) {
     die("Error in SQL query: " . pg_last_error());
 }       

 // iterate over result set
 // print each row
 while ($row = pg_fetch_array($result)) {
     echo "Country code: " . $row[0] . "<br />";
     echo "Country name: " . $row[1] . "<p />";
 }       

 // free memory
 pg_free_result($result);       

 // close connection
 pg_close($dbh);      
?>

【问题讨论】:

  • 你遇到了什么困难?
  • 它给了我这个错误:Error in SQL query: ERROR: relation "aircraft" does not exist at character 15. Aircraft 是我的一个模式中的一个表。
  • 表名是aircraft还是"Aircraft"?更多关于混合大小写标识符的信息:stackoverflow.com/questions/8792912/…

标签: php database postgresql


【解决方案1】:

用架构名称限定表

select *
from my_schema.aircraft

【讨论】:

  • 我收到此错误 - Error in SQL query: ERROR: schema "aircraft" does not exist at character 15
【解决方案2】:

使用:
将搜索路径设置为 myschema;或

SET search_path TO myschema, myschemab;

https://www.postgresql.org/docs/9.4/static/ddl-schemas.html

【讨论】:

    【解决方案3】:

    架构限定表名为Clodoaldo already advised。或者将 search_path 设置为永久效果。它的工作原理很像文件系统中的搜索路径。

    如何永久取决于你如何设置它。见:

    【讨论】:

    • @Clodoaldo 的方法似乎由于某种原因不起作用。我的PHP代码。我才刚刚开始使用 SQL n PHP,所以我不太了解你的程序。我现在正在编辑我的问题以合并我的代码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-24
    • 2021-11-15
    • 2019-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-18
    相关资源
    最近更新 更多