1

I'm using postgreSQL 9.1 with postGIS 2.0 .Also Apache 2.2.22 and PHP 5.3.13. I'm trying to set PHP's PDO for postgreSQL. I also use Windows 7. According to this this I am ok, I checked my php.ini and I found this line extension=php_pgsql.dll

But according to this (runtime configuration) I have to set a dsn in my php.ini. I dont actually get what I have to do. And why. I try to write something like pdo.dsn.'pgsql:host=localhost;port=5432', in php.ini and then I restarted the Apache.

After that I created a new php file to test PDO. This is the code

 try { $DBH = new PDO("pgsql:host=localhost;port=0000;dbname=blah;user=blah;password=blah"); $DBH->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING ); } catch(PDOException $e) { echo" sorry man! "; echo $e->getMessage(); } $name="frouto"; $STH = $DBH -> prepare("SELECT * FROM controller WHERE c_name LIKE :name;"); $STH->bindParam(':name', $name); $STH->execute(); $result = $STH->fetchAll(PDO::FETCH_ASSOC); 

Just really simple to test PDO. This does not work at all. Nor an exception error is printed. I guess runtime configuration is wrong. And dsn is also wrong. What I have to do to fix them?

1 Answer 1

1

First, make sure that postgres is running on your localhost.

Second, make sure you can connect to postgres: telnet localhost 5432

Third, Postgres typically runs on port 5432. Your code is connecting on 0000.

The DSN is the (only) argument in your PDO creation:

$DBH = new PDO("pgsql:host=localhost;port=0000;dbname=blah;user=blah;password=blah"); 

i.e.

$dsn = "pgsql:host=localhost;port=0000;dbname=blah;user=blah;password=blah"; $DBH = new PDO($dsn); 

check each of these steps, and ask again how to resolve if one of them fails and you are unable to figure that step out.

Sign up to request clarification or add additional context in comments.

4 Comments

@ Zak Hi. Yes I checked and runs OK. I can connect by using pg_connect so I guess I am OK. And yes, the actuall port is 5432, I just chose to "hide" that , along with username, passowrd etc. So, just to clarify, I dont have to add anything in my php.ini file? Then what's the problem? I dont get it
can you also run your query using the relevant login credentials from the command line? Ensure you have permission to run the query.
also, do a print_r on $results.. what does that say?
I re-edit my connection by rewriting the "try" part as follow $dsn= "pgsql:host=localhost;port=5432;dbname=blah;user=blah;password=blah"; $DBH = new PDO($dsn); $DBH->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING ); echo"conn ok"; . I also used this print_r($STH);. The "try" part prints "conn ok" but the print_r and the query still dont work.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.