HTTP ERROR 500 (pls help)

So I’m running Ubuntu 18.04 on a Virtual Machine and I’m trying to have two docker containers interact with one another. I have php:apache installed one container and postgresql on the other. I’ve created a table on postgresql, however, when I try to use a php script to pull or insert data into the table, I get an error. Can someone help me resolve this?

PHP Script below:

<?php

$db_host = "localhost";
$db_username = "postgres";
$db_password = "kha";
$db_name = "postgres";

$con = pg_connect($db_host,$db_username,$db_password,$db_name);

if ($con->connect_error) {
  die("Database connection failed: " . $con->connect_error);
}

if(isset($_POST['submit'])) {
  $id=$_POST['id'];
  $password=$_POST['password'];

  $query = "INSERT INTO test.table1 (id, password)
  VALUES ('$id', '$password')";

    if (!pg_query($con, $query)) {
        die('An error occurred. Your review has not been submitted.');
    } else {
      echo "Thanks for your review.";
    }

}
?>

Do you have php’s error_reporting set to E_ALL and display_errors set to ON, in the php.ini on your system, so that php would help you by reporting and displaying all the errors it detects?

HTTP 500 errors for server-side languages are usually due to fatal parse or fatal run-time errors. I suspect a fatal run-time error in your case, about the pg_connect() function not existing due to that extension not being installed/enabled on you php installation.

These are my error_reporting settings:

; error_reporting
; Default Value: E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED
; Development Value: E_ALL
; Production Value: E_ALL & ~E_DEPRECATED & ~E_STRICT

display_errors:

; display_errors
; Default Value: On
; Development Value: On
; Production Value: Off

I think they are on. How would I go about enabling pg_connect? I appreciate you taking the time to respond :smile:

Those are just comments showing suggested values. The actual settings are farther down in the file. You should also use a phpinfo(); statement in a .php script file to check what the values actually are (in case the php.ini that you are changing is not the one the php is using.)

Ooh, I see my bad. The phpinfo script says that display_errors local value and master value are off. error_reporting is set to 22527 for local and master values… is that correct? In order to change these I need to go deeper into the file, is the correct place to change the values obvious? I don’t want to mess anything up. :sweat_smile:

Oh okay, I did ctrl+w to find the display_errors=off and changed it to on and then restarted apache to refresh the file.

The lines setting the values will be assignment statements -

error_reporting=E_ALL
display_errors=On

Stop and start the web server to get any changes made to the php.ini to take effect and recheck using the phpinfo().

display_errors=On in the phpinfo file now, but error_reporting now = 32767. Is that how it should look? I set it to only E_ALL in the php.ini file.

Yes. That’s the current E_ALL value, which sets all bits.

1 Like

Okay, so now when I try to open a php file it says this:

Fatal error : Uncaught Error: Call to undefined function pg_connect() in /var/www/html/table.php:7 Stack trace: #0 {main} thrown in /var/www/html/table.php on line 7

So I need to enable pg_connect?

The whole PostgreSQL extension will need to be installed.

Except for some past testing, I haven’t use this recently. You may instead want to use the PDO PostgreSQL extension, as the PDO extension simplifies and provides a common programming interface for 12+ different database types. https://www.php.net/manual/en/ref.pdo-pgsql.php

When I attempt to do “apt-get install php-pgsql” it returns:

Reading package lists… Done
Building dependency tree
Reading state information… Done
Package php-pgsql is a virtual package provided by:
php7.0-pgsql 7.0.33-0+deb9u3 [Not candidate version]
php7.3-pgsql 7.3.11-1~deb10u1 [Not candidate version]

E: Package ‘php-pgsql’ has no installation candidate

and trying to install one of those results in:

root@a60b9353291e:/var/www/html# apt-get install php7.0-pgsql 7.0.33-0+deb9u3
Reading package lists… Done
Building dependency tree
Reading state information… Done
Package php7.0-pgsql is not available, but is referred to by another package.
This may mean that the package is missing, has been obsoleted, or
is only available from another source

E: Package ‘php7.0-pgsql’ has no installation candidate
E: Unable to locate package 7.0.33-0+deb9u3
E: Couldn’t find any package by glob ‘7.0.33-0+deb9u3’
E: Couldn’t find any package by regex ‘7.0.33-0+deb9u3’
root@a60b9353291e:/var/www/html# apt-get install php7.3-pgsql 7.3.11-1~deb10u1
Reading package lists… Done
Building dependency tree
Reading state information… Done
Package php7.3-pgsql is not available, but is referred to by another package.
This may mean that the package is missing, has been obsoleted, or
is only available from another source

E: Package ‘php7.3-pgsql’ has no installation candidate
E: Unable to locate package 7.3.11-1~deb10u1
E: Couldn’t find any package by glob ‘7.3.11-1~deb10u1’
E: Couldn’t find any package by regex ‘7.3.11-1~deb10u1’

Sorry, I’m pretty new to this - what am I supposed to do to install the packages?

Also, I have no idea what to do with this either “–with-pdo-pgsql”

It keeps saying command not found…

Sponsor our Newsletter | Privacy Policy | Terms of Service