1

I am trying to create a simple php application on heroku with postgresql back end. I have created a connection to postgresql server with the provided credentials from heroku:-

<?php

class DB_Connect
    {
        private $db; 
        public function connect()
        {
          $host        = "";
          $port        = "port=5432";
          $dbname      = "";
          $credentials = "user= password=";

          $db = pg_connect( " $url $host $port $dbname $credentials"  );
          if(!$db){
             echo "Error : Unable to open database\n";
          } else {
             echo "Opened database successfully\n";
          }
        }
    }

    $db1 = new DB_Connect();
    $conn = $db1->connect();

    $query = "insert into public.user_test(name,email) values('val','[email protected]')";
    $result = pg_query($conn,$query);
    echo $result;
?>

When i deploy the app on heroku, I get a Opened Database successfully message but nothing gets inserted into the table when I check the table using pgadmin.

I tried it with and without the schema name "public", but that didn't help either.

When I run the app locally by providing the local postgresql credentials, the table gets populated.

1
  • 1
    Just note that the $db from $db = pg_connect.. is not the same as the one from private $db. Commented Feb 29, 2016 at 14:30

1 Answer 1

1

You didn't return the connection in your connect method. Adding return $db; after the if condition should do the trick.

class DB_Connect
{
    public function connect()
    {
      $host        = "";
      $port        = "port=5432";
      $dbname      = "";
      $credentials = "user= password=";

      $db = pg_connect( " $url $host $port $dbname $credentials"  );
      if(!$db){
         echo "Error : Unable to open database\n";
      } else {
         echo "Opened database successfully\n";
      }
      return $db;
    }
}
Sign up to request clarification or add additional context in comments.

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.