I try to explain the problem I have!!!
I use PDO extension to connect to PostgreSQL through pgpool-II. It works fine within Apache, but from PHP CLI (on the same machine) I receive this PDO error:
SQLSTATE[HY000]: General error: 7 no connection to the server
I have already searched on Google and here, but it seems that no one has ever tried to do this. Does anyone have any idea?
EDIT:
This is the code I use to establish a connection:
include 'manage_db.php';
include_once 'properties.php';
global $properties;
$dsn = 'pgsql:dbname=' . $properties['db_pgpool'] . ';host=localhost;port=' . $properties['port_pgpool'];
try{
$mgmtDb = new ManageDb($dsn, $properties['username_pgpool'], $properties['password_pgpool']);
} catch (Exception $e) {
echo 'PDO - Caught exception: ', $e->getMessage(), "\n";
}
ManageDB is my own class that implements some utility functions as well as create the database connection:
class ManageDb {
var $db;
function ManageDb($dsn, $username, $password){
$this->db = new PDO($dsn, $username, $password);
$this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
....
Try this
config.database.php
<?php
class DatabaseConfig {
const DBNAME = 'dbname';
const HOST = '123.1.233.123';
const USER = 'mysuperuser';
const PASSWORD = 'mysupperparrword';
const PORT = 5432;
}
?>
class.database.php
<?php
include('config.database.php');
class Database {
protected static $instance = null;
final private function __construct() {}
final private function __destruct() {
self::$instance = null;
}
final private function __clone() {}
public static function getInstance() {
if (self::$instance === null) {
try {
self::$instance = new PDO(
'pgsql:host=' . DatabaseConfig::HOST .
';port=' . DatabaseConfig::PORT .
';dbname=' . DatabaseConfig::DBNAME .
';user=' . DatabaseConfig::USER .
';password=' . DatabaseConfig::PASSWORD
);
self::$instance->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
self::$instance->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
} catch (PDOException $e) {
die('Database connection could not be established.');
}
}
return self::$instance;
}
public static function __callStatic($method, $args) {
return call_user_func_array(array(self::instance(), $method), $args);
}
}
?>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With