PHP程序无法连接到Postgresql数据库
我的PHP程序正在尝试连接到另一台机器上的PostgreSQL DB,但当网页加载时,它只会说“PHP程序无法连接到Postgresql数据库,php,apache,postgresql,Php,Apache,Postgresql,我的PHP程序正在尝试连接到另一台机器上的PostgreSQL DB,但当网页加载时,它只会说“找不到驱动程序”” 我搜索了一下,发现我不得不取消注释extension=php\u pdo\u pgsql.dll 和php.ini中的extension=php\u pgsql.dll,我已经这么做了。我不知道我还能错过什么。请导游 database.php <?php class Database { private static $dbName = 'istore-db' ;
找不到驱动程序”
”
我搜索了一下,发现我不得不取消注释extension=php\u pdo\u pgsql.dll
和php.ini
中的extension=php\u pgsql.dll
,我已经这么做了。我不知道我还能错过什么。请导游
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;
}
}
?>
<!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>
index.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;
}
}
?>
<!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>
PHP积垢网格
身份证件
名称
描述
活跃的
行动
更改此行:
self::$cont = new PDO( "pgsql:host=".self::$dbHost.";"."dbname=".self::$dbName, self::$dbUsername, self::$dbUserPassword);
到
你试过重新启动你的Web服务器吗?出于好奇,操作系统是什么?DLL文件只对Windows安装有用。是的,我重新启动了我的Web服务器,操作系统是Windows 7。我们是否必须在部署期间添加任何其他驱动程序文件。我问这个问题是因为我没有添加任何内容。我刚刚将.php文件从我的开发机器复制到Apache web服务器的htdocs文件夹中。你有什么进展吗?@Adrian:我有点离开了这个宠物项目,因为我没有在这方面取得任何进展。在重新启动web服务器后尝试了你提供的新字符串,但仍然没有成功:(有趣的是,我有一个java程序和PostgreSQL管理器工具,它们都可以使用相同的凭据、主机名和数据库名连接到远程PostgreSQL server。@user2325154如果您可以使用凭据连接,忘记将行更改为localhost,只更改self:;$cont…查看我的编辑我看不出我们之间有任何区别在你之前发布的新行和旧行中添加。但是,我已经复制粘贴了你的建议,仍然没有成功。请输入$dbHost名称,并与你的PostgreSQL管理器工具连接