简体   繁体   English

无法在PDO中进行PG连接

[英]can't get PG connection going in PDO

I am unable to connect to my database. 我无法连接到数据库。 Sure I'm just missing something simple: 当然,我只是缺少一些简单的东西:

$host = "localhost";
$port = "5432";
$db_name = 'db';
$username = "user";
$password = "pass";
$dbh = new PDO("pgsql:dbname=$db_name; host=$host", $username, $password );

$dbh->setAttribute( PDO::ATTR_ERRMODE , PDO::ERRMODE_EXCEPTION );

$sth = $dbh->prepare("
    SELECT *
    FROM   test1
    WHERE  id = :id
    ");

$sth->bindValue(':id', 1, PDO::PARAM_STR);
$sth->execute();

$result = $sth->fetchAll(PDO::FETCH_ASSOC);
var_dump($result);

I get the error 我得到错误

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42P01]: Undefined table: 7 ERROR: relation "test1" does not exist LINE 3: FROM test1 ^' in ...[url]... address Stack trace: #0 ...[url]... PDOStatement->execute() #1 {main} thrown in ...[url] 致命错误:消息为'SQLSTATE [42P01]的未捕获异常'PDOException':未定义表:7错误:... [url] ...中的关系“ test1”不存在第3行:FROM test1 ^'...地址堆栈跟踪: #0 ... [url] ... PDOStatement-> execute()#1 {main}抛出... [url]

Thanks! 谢谢!

Maybe try this: 也许试试这个:

$host = 'localhost';
$port = '5432';
$db_name = 'db';
$username = 'user';
$password = 'pass';

$dbh = new PDO('pgsql:port=' . $port . ';host=' . $host . ';dbname=' . $db_name . ';charset=utf8', '' . $username . '', '' . $password . '');
  1. try using this DSN Format: "pgsql:host=localhost;port=5432;dbname=db;user=user;password=pass". 尝试使用以下DSN格式:“ pgsql:host = localhost; port = 5432; dbname = db; user = user; password = pass”。
  2. check that your user has all the privileges it needs on postgresql to access the schema, DB and that table. 检查您的用户是否具有在postgresql上访问架构,数据库和该表所需的所有特权。 "grant select on table test1 TO user" should give you access for that table. “向表test1 TO用户授予选择权”应授予您对该表的访问权限。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM