pg_connect working as expected in login.php file, when the same code is put in connection.php and require_once used it doesn't function

Viewed 27

connection.php has contents

<?php
   $host        = "host=127.0.0.1";
   $port        = "port=5432";
   $dbname      = "dbname=database_name";
   $credentials = "user=database_user_ password=userPAssworD";
   $conn = pg_connect( "$host $port $dbname $credentials" );
   if(!$conn) {
      echo "Error : Unable to open database\n";
      echo $conn;
      exit;
   } 
?>

The code

   $host        = "host=127.0.0.1";
   $port        = "port=5432";
   $dbname      = "dbname=database_name";
   $credentials = "user=database_user_ password=userPAssworD";
   $conn = pg_connect( "$host $port $dbname $credentials" );

written in login.php functions and including instead

require_once(connection.php);

with the files in the same folder doesn't work. That is when I call later in login.php something like

pg_prepare($conn, "valid_user", "select * from users WHERE username = $1");                          
$result = pg_execute($conn, "valid_user", array($post['username'],));                                                                                         

$conn is not available (null).

I'm sure I am missing something obvious that I don't see. Any help or pointers in the right direction would be very welcome.

0 Answers
Related