Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute:

I have had the hardest time getting PHP to talk to Postgres SQL. Here is my setup:

Ubunu Desktop 13.10
PHP 5.5.3
Postgres 9.1.10
Apache 2.4.6
Netbeans 7.4 with xdebug

Everything is working fine. I am able to enter and retrieve data in the Postgres Database fine from the command line but not in PHP. Here are the lines of code that I am using to connect:

$dbConn = new softwareDB('localhost', 'postgres', 'root', 'softwareuitest');
...
  $results = $dbConn.getClients();
    while($client = pg_fetch_result($results)){
        echo '<option value=\"'.$client.'\">'.$client.'</option>';
    }

The softwareDB class constructor is as follows:

Class softwareDB {

private $conn;

function _construct($host, $user, $password, $dbname) {
    $connectString = 
            'host=' . $host .
            ' port=5432' . 
            ' user=' . $user .
            ' password=' . $password .
            ' dbname' . $dbname;

    $this->conn = pg_connect($connectString);
}
...
public function getClients() {
    global $conn;
    return pg_query($conn,'getClients','SELECT * FROM clients');
}
...
}

When running the code nothing happens... I see nothing in the Apache log file, nothing in the postgres log, nothing from the debugger, and only HTML (without query data) in the output.

I cannot post images yet but here are the details about Postgres from phpInfo():

PDO
PDO Drivers | pgsql

pdo_pgsql
version 9.1.9
module 1.0.2
revision $id$

pgsql
PostgreSQL(libpq) | PostgreSQL 9.1.9 on x86_64-unknown-linux-gnu, compiled by gcc (Ubuntu/Linaro 4.8.1-10ubuntu1) 4.8.1, 64-bit allow_persistent is on

share|improve this question
    
Gah, why would you use global $conn? Just use $this->conn. Also, PHP uses the arrow operator (->) to reference class instance methods, not the dot so it should be $dbConn->getClients() – Phil Nov 13 '13 at 2:15
    
A little new to PHP here. I fully agree. Will be changed immediately! – Forest-G Nov 13 '13 at 2:17
    
Also, where's your error checking for connection failures? – Phil Nov 13 '13 at 2:17
    
I have these lines right below the call to softwareDB: if(!$dbConn){ die(pg_last_error()); } – Forest-G Nov 13 '13 at 2:20
    
Another thing, pg_query only takes 2 arguments (not 3) and pg_fetch_result takes 2 or 3 (certainly not 1). I suggest you read the manual pages for the functions you're using – Phil Nov 13 '13 at 2:42

1 Answer 1

You have two main issues. The first is a typo; PHP class instance methods are called via the arrow operator. It should be...

$results = $dbConn->getClients();

The second is your use of global $conn. You don't have a variable named $conn, global or not. What you do have is a member property $conn which you may reference by prefixing it with $this, eg

public function getClients() {
    return pg_query($this->conn, 'SELECT * FROM clients');
}

Looking further, I would suggest that if you're going to abstract DB details away in your softwareDB class, you might as well do it completely. I'd go with something like this

class softwareDB {
    private $conn;

    public function __construct($host, $user, $password, $dbname) {
        $this->conn = pg_connect(sprintf('host=%s port=5432 user=%s password=%s dbname=%s',
            $host, $user, $password, $dbname));
        if ($this->conn === false) {
            throw new Exception(pg_last_error());
        }
    }

    public function getClients() {
        $result = pg_query($this->conn, 'SELECT * FROM clients');
        return pg_fetch_all($result);            
    }
}

I'm not too familiar with the PostgreSQL Functions so you may need to tweak this but the general idea is that code using your softwareDB class doesn't need to know anything about postgres or other DB operations.

share|improve this answer
    
All good suggestions. Many of the error reporting things have been hastily added in an attempt to get any heartbeat from Postgres. I have corrected the error (dot operator and importing the global) but I am getting nothing at all saying that there was a connection to the DB. The debugger just passes on over the function and returns no values for it as if nothing was out of the ordinary. – Forest-G Nov 13 '13 at 2:50

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.