1

I was trying to connect my PHP page to PostgreSQL, but it's not working. The code which I have tried is given below:

<?php

 $connection = pg_connect("host=localhost dbname=mydb user=postgres password=pgsql"); 

 if (!$connection)
 { 
   echo "Couldn't make a connection!"; 
 } 

?>

During the time of installation, the system asked me for a password and I had gives as pgsql. My database name is "mydb". Somebody please help me.

Thanks and regards.

tismon

2
  • @tismon; do you get any error message ? Commented Feb 8, 2010 at 8:40
  • 1
    A db connection failure should result in a PHP warning with an error message. If it's not printed on the page, check your error log and post the error message here. Commented Feb 8, 2010 at 8:41

4 Answers 4

1

I advice you to take a look at pg_last_error, it would give you valuable info on what is going wrong

i.e.

if(!$connection){
 die(pg_last_error($connection));
}
Sign up to request clarification or add additional context in comments.

2 Comments

i didnt get any error message even it doesnot printing the other simple echo after the connection string :(
like pointed by max you should have something in apache log, if you have shell access to the server you can try to use psql and see if you can connect like expected.
1

The error message i got after troubleshooting is

"pgsql module unavailable"

I set the

extension=php_pdo_pgsql.dll extension=php_pgsql.dll

and extension_dir = "C:\php\ext"

in the php.ini file.But it was of no use.

I use postgresql 8.3 and php 5.2.12 threadsafe with apache 2.2 in win xp...

any help will be deeply appreciated..

Tismon

Comments

1

its solved.. the problem is with the php version. i had installed php5.2.6 and replced the pgsql.dll extension with that of php5.2.5

Thanks a lot VolkerK,RageZ and Max S

regards

tismon

Comments

0

Maybe the pgsql module isn't installed/loaded?

<?php
echo "<pre>If this line isn't printed you have a parse error in your script</pre>\n"; flush();
error_reporting(E_ALL); ini_set('display_errors', true);    
extension_loaded('pgsql') || die('pgsql module unavailable');


$connection = pg_connect("host=localhost dbname=mydb user=postgres password=pgsql"); 
if (!$connection) { 
  echo "Couldn't make a connection! ". pg_last_error(); 
}
else {
  echo 'connected to server';
}
flush();
?>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.