5

I posting this question as answers in previews posts didn't help me. i am trying to connect to DB postgres which is located on remote server from my local computer. I am using Xampp and Netbeans IDE.

below is the code i am trying to run :

        //attempt a connection
    $dbh = pg_connect("host=server_name port=5432 dbname=prototype1 user=leonidz password=****");
    if (!$dbh) {
    die("Error in connection: " . pg_last_error());
               }

This is the error msg:

Fatal error: Call to undefined function pg_connect() in C:\xampp\htdocs\PhpProject1\index.php on line 10

in php.ini I uncommitted: extension=php_pgsql.dll and extension=php_pdo_pgsql.dll

i also mapped the extension_dir = "C:\xampp\php\ext\" and restarted the Apache server after the change.

I don't know what else i should do, please help.

4
  • 1
    What does PHP info tell you of the loaded extensions? What error does apache log give you when restarting? Commented Mar 30, 2013 at 19:05
  • I have any error in the Apache error log, PHP Warning: PHP Startup: Unable to load dynamic library 'C:\\xampp\\php\\ext\\php_pdo_pgsql.dll' - The specified module could not be found.\r\n in Unknown on line 0 when i try to run the function to connect to the DB i am getting: PHP Fatal error: Call to undefined function pg_connect() in C:\\xampp\\htdocs\\PhpProject1\\index.php on line 10 PHP INFO: there is no results when i try to find "pgsql" string in php info Commented Mar 31, 2013 at 20:52
  • what OS are you using? is it Linux or Windows environment? what version are you using, x86 or x64? Which php.ini did you edit? remember it's the one from xampp/apache/bin/ Hope you answer all my questions so we can help you better. Cheers Commented Apr 10, 2013 at 10:08
  • problem solved by downgrading xampp version to v3.1.0 Commented Apr 13, 2013 at 12:17

4 Answers 4

15

In the C:\xampp\php\php.ini , uncomment these:

extension=php_pdo_pgsql.dll
extension=php_pgsql.dll

After Done this Need to restart your xampp

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

5 Comments

Worked like a charm.
I am facing the same problem but couldn't find the above lines in php.ini file.
THANK YOU! I was able to configure xampp in 5 minutes to learn other 10 minutes PostgreSQL .. but remember: in php 7 php.ini there are line 905- extension=pdo_pgsql and line 907-extension=pgsql
I am using xampp 5.6.38, and it was in line no. 908 and 910. But why these lines are commented by default?
AND extension="pgsql.so" in case of running xampp on linux
6

Go to PHP dir oepn php.ini file, there you will find these two lines, just remove the semicolon from the left hand side, save the file. Next, stop you xampp server and restart it again it will works fine

;extension=pdo_pgsql
;extension=pgsql

Comments

5

In php.ini you need to uncomment

extension=php_pdo_pgsql.dll

extension=php_pgsql.dll

AND

extension="pgsql.so"

Comments

0

Fatal error: Uncaught Error: Call to undefined function pg_connect() in C:\xampp\htdocs\postgresql_dbcon\connection1.php:5 Stack trace: #0 C:\xampp\htdocs\postgresql_dbcon\view.php(4): include() #1 {main} thrown in C:\xampp\htdocs\postgresql_dbcon\connection1.php on line 5

connection1.php file

<?php
    $db = pg_connect("host=localhost port=5432 dbname=db_test user=postgres password=123abc");//line 5
    if($db){
        echo "Connected\n";
    } else {
        echo "Error NotConnected\n";
    }
?>

view.php file

<?php include "connection1.php";?>
<?php 
    $query = 'Select * FROM table2';
    $result = pg_query($db, $query);
    if (!$result) {
  echo "An error occurred.\n";
  exit;
}
while ($row = pg_fetch_row($result)) {
  $dbdata[]=$row;
}
$myJSON = json_encode($dbdata);
    echo $myJSON;
?>

If you are using XAMPP in Windows Operating System then go to directory where you have installed XAMPP. Follow the path i.e xampp/php/php.ini . Open php.ini file in your editor and search for ";extension=pdo_mysql" and replace it with "extension=pdo_mysql" and also replace ";extension=pdo_pgsql" with "extension=pdo_mysql". Then save the file and restart your XAMPP.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.