PHP程序无法连接到Postgresql DB

时间:2015-02-26 21:56:51

标签: php apache postgresql

我的PHP程序正在尝试连接到位于另一台计算机上的PostgreSQL数据库,但是当网页加载它时只是说“Could not find driver”。

我谷歌四处发现我必须取消注释extension=php_pdo_pgsql.dll 我已经做过的extension=php_pgsql.dll php.ini。不确定我还能错过什么。请指导。

database.php中

<?php
class Database
{
    private static $dbName = 'istore-db' ;
    private static $dbHost = 'gsi-547576.gsiccorp.net' ;
    private static $dbUsername = 'postgres';
    private static $dbUserPassword = 'postgres';
    private static $dbPort = '5432';

    private static $cont  = null;

    public function __construct() {
        die('Init function is not allowed');
    }

    public static function connect()
    {
       // One connection through whole application
       if ( null == self::$cont )
       {     
        try
        {
          //self::$cont =  new PDO( "pgsql:host=".self::$dbHost.";"."dbname=".self::$dbName, self::$dbUsername, self::$dbUserPassword); 
          self::$cont = new PDO("pgsql:dbname=" . self::$dbName . ";host=" .self::$dbHost . ";port=" .self::$dbPort, self::$dbUsername,  self::$dbUserPassword);
        }
        catch(PDOException $e)
        {
          die($e->getMessage()); 
        }
       }
       return self::$cont;
    }

    public static function disconnect()
    {
        self::$cont = null;
    }
}
?>

的index.php

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <link   href="css/bootstrap.min.css" rel="stylesheet">
        <script src="js/bootstrap.min.js"></script>
    </head>
    <body>
        <div class="container">
            <div class="row">
                <h3>PHP CRUD Grid</h3>
            </div>
            <div class="row">
                <p>
                    <a href="create.php" class="btn btn-success">Create</a>
                </p>
                <table class="table table-striped table-bordered">
                    <thead>
                        <tr>
                            <th>ID</th>
                            <th>Name</th>
                            <th>Description</th>
                            <th>Active</th>
                            <th>Action</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php
                        include 'database.php';
                        $pdo = Database::connect();
                        $sql = 'SELECT * FROM categories ORDER BY  NAME';
                        foreach ($pdo->query($sql) as $row) {
                            echo '<tr>';
                            echo '<td>' . $row['category_id'] . '</td>';
                            echo '<td>' . $row['name'] . '</td>';
                            echo '<td>' . $row['description'] . '</td>';
                            echo '<td>' . $row['active'] . '</td>';
                            echo '<td width=250>';
                            echo '<a class="btn" href="read.php?id=' . $row['id'] . '">Read</a>';
                            echo ' ';
                            echo '<a class="btn btn-success" href="update.php?id=' . $row['id'] . '">Update</a>';
                            echo ' ';
                            echo '<a class="btn btn-danger" href="delete.php?id=' . $row['id'] . '">Delete</a>';
                            echo '</td>';
                            echo '</tr>';
                        }
                        Database::disconnect();
                        ?>
                    </tbody>
                </table>
            </div>
        </div> <!-- /container -->
    </body>
</html>

1 个答案:

答案 0 :(得分:0)

更改此行:

self::$cont =  new PDO( "pgsql:host=".self::$dbHost.";"."dbname=".self::$dbName, self::$dbUsername, self::$dbUserPassword); 

self::$cont = new PDO("pgsql:dbname=" . self::$dbName . ";host=" .self::$dbHost, self::$dbUsername,  self::$dbUserPassword);