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
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:15if(!$dbConn){ die(pg_last_error()); }
– Forest-G Nov 13 '13 at 2:20pg_query
only takes 2 arguments (not 3) andpg_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