I can’t connect with postgresql in php

Asked

Viewed 4,717 times

1

I’m trying to make a PHP Login system using Postgresql but I can’t get my application to connect to the database. follows the code:

 <?php
     $connect = pg_connect("dbname=testebd"); //Banco de dados previamente criado
     if (!$connect)
     {
        echo "Conexão não realizada";
     } else 
     {
        echo "Conexão bem sucedida";
     }
?>

When I run my application, absolutely nothing happens. The page is completely blank. I can’t even print the contents of the variable:

$connect = pg_connect("dbname=testebd");
echo "$connect";

Can anyone tell me why the connection is not being realized. Note: I use the Apache server on an Ubuntu and can normally use the postegreSQl by bash.

  • enable php error messages

  • Blank page and error 500, mean programming error. Enable error display by adding these two lines at the beginning of the file, ini_set('display_errors', true); error_reporting(E_ALL);

  • Managed to solve?

  • Now the error that is appearing when I do if(!($connection= pg_connect ("host=localhost dbname=iluminecar port=5432 user=sullyvan password="))) { print "Could not establish a connection to the database." ; } Else { pg_close ($connected); print "Connection OK!" ; } is Fatal error: Call to Undefined Function pg_connect() in /var/www/html/index.php on line 24

  • 1

    I managed to solve, installed php5-pgsql and set display_errors = on

3 answers

1


1º Enable your PHP error messages:

Open this file with your preferred editor: /etc/php5/apache2/php.ini

Look for this line:

display_errors = Off

and exchange for this:

display_errors = On

After that, restart apache (in the terminal):

service apache2 restart

Based on this, take the error message and put here.

But, to advance, follows an example of connection with the postgres:

<?php
if(!@($conexao=pg_connect ("host=HOST dbname=BANCO port=5432 user=LOGIN password=SENHA"))) {
   print "Não foi possível estabelecer uma conexão com o banco de dados.";
} else {
   pg_close ($conexao);
   print "Conexão OK!"; 
}
?>

0

Will not connect this way, it is necessary to inform the full dsn, with host, user and password beyond the database.

$connect = pg_connect("host=localhost dbname=teste user=usario password=senha");

0

The PHP 7.1 version is unstable and full of bugs (more than 30 error pages on the php site). One of these errors is that PHP cannot see the modules needed to execute pg_connect or pgsql PDO commands. And it is not a simple configuration of php.ini file, but an internal error. To connect with postgresql you need to go back to version 5.x.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.