dongzhan1383 2019-04-30 23:18
浏览 30

使用PDO创建php数据库连接的正确方法是什么?

Background

I have recently been put in charge of maintaining an almost completed website and backend that is written in 6 or so languages (HTML, javascript, php, java, postgres, python...). I'm good with some of these, but not others (php, javascript).

The Problem

I can't connect the website to the postgres database with php. I've found and modified a file called dbconn.php (which presumably is responsible for connecting to the database) and it looks like this currently:

<?php
    $host = "redshift-cluster-merp.something.region.redshift.amazonaws.com";
    $port = 5430;
    $user = "awsuser";
    $pass = "myfancypassword";
    $dbName = "merpdev";

    $dsn = "pgsql:host=$host;dbname=$dbName;port=$port;";
    $pdo = new PDO($dsn, $user, $pass);
?>

This doesn't create a connection between the website and database. However, I am able to log into the database from the same ip address in terminal via

psql -h redshift-cluster-merp.something.region.redshift.amazonaws.com -U awsuser -p 5430 -d merpdev

And I can also connect via JDBC with similar parameters. Is the file dbconn.php correct or am I missing something?

I imagine the lines with $dsn and/or $pdo might have a syntactic error.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
    • ¥20 关于URL获取的参数,无法执行二选一查询
    • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
    • ¥15 marlin编译错误,如何解决?
    • ¥15 有偿四位数,节约算法和扫描算法
    • ¥15 VUE项目怎么运行,系统打不开
    • ¥50 pointpillars等目标检测算法怎么融合注意力机制
    • ¥20 Vs code Mac系统 PHP Debug调试环境配置
    • ¥60 大一项目课,微信小程序
    • ¥15 求视频摘要youtube和ovp数据集