我无法连接到我的数据库。当然,我只是遗漏了一些简单的东西:
$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);
我收到错误
致命错误:带有消息的未捕获异常'PDOException' 'SQLSTATE [42P01]:未定义的表:7错误:关系“test1”没有 存在LINE 3:FROM test1 ^'in ... [url] ...地址堆栈跟踪:#0 ... [url] ... PDOStatement-> execute()#1 {main}抛出... [URL]
谢谢!
答案 0 :(得分:1)
也许试试这个:
$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 :(得分:0)