Brandon J
Brandon J

Reputation: 73

How do I start a PGSQL connection in PHP?

I am trying to start a pgsql connection in php but i get pg_last_error(): No PostgreSQL link opened yet Please forgive my amateur question as i am a bit new to php.

Here is my php code:

<?php

$connect = pg_connect("host=xxx.xx.xxx.21 dbname=d106 user=b16 password=bran") or die("Could not connect: " . pg_last_error());

$result = pg_query($connect,"SELECT distinct thestartgeom FROM bikes");
if (!$result)
{
echo "no results ";
}

while($row = pg_fetch_array($result))
{

 $coor = $row['thestartgeom'];
echo $coor;

}

pg_close($connect);
?>

Upvotes: 0

Views: 2952

Answers (2)

Daniel V&#233;rit&#233;
Daniel V&#233;rit&#233;

Reputation: 61676

When pg_connect fails, it returns FALSE and produces a PHP warning with the detailed information on why it couldn't initiate the connection. If you can see the other message:pg_last_error(): No PostgreSQL link opened yet that you're reporting, I'd expect you should be able to see the previous one too, which is the one normally telling the reason of the failure.

If the display_errors configuration setting is set to 0, the first message would not show up on the browser/screen but the second would not either.

Anyway, assuming you can't have access to pg_connect warnings for whatever reason such as custom error handler, what's wrong with your code is that pg_last_error() must have an already opened connection to work.

To access the detailed error message from a failed pg_connect, the built-in PHP function error_get_last() (returning an array) could be used.

<?
$connect= pg_connect("your-connect-string");
if (!$connect) {
  print_r(error_get_last());
  // for only the message:
  // echo error_get_last()['message']
}
die("DB connection failed");
?>

See also how to catch pg_connect() function error? if you prefer exceptions.

Upvotes: 2

chalitha geekiyanage
chalitha geekiyanage

Reputation: 6872

In your pgsql connection you have missed the port number. Try this way.

<?php
$host        = "host=xxx.xx.xxx.21";
$port        = "port=5432";
$dbname      = "dbname=d106";
$credentials = "user=b16 password=bran";

$connect= pg_connect( "$host $port $dbname $credentials"  ) or die("Could not connect: " . pg_last_error());


$result = pg_query($connect,"SELECT distinct thestartgeom FROM bikes");
if (!$result)
{
echo "no results ";
}

while($row = pg_fetch_array($result))
{

 $coor = $row['thestartgeom'];
echo $coor;

}

pg_close($connect);
?>

You can store PgSQL connection code in one PHP file to reuse

pgsql_db_connection.php file

<?php
$host        = "host=xxx.xx.xxx.21";
$port        = "port=5432";
$dbname      = "dbname=d106";
$credentials = "user=b16 password=bran";

$connect= pg_connect( "$host $port $dbname $credentials"  );
if(!$connect){
  echo "Error : Unable to open database\n";
}
?>

Call pgsql_db_connection.php file in other php files to use your database connection.

<?php
require_once('pgsql_db_connection.php');

 $result = pg_query($connect,"SELECT distinct thestartgeom FROM bikes");
if (!$result)
{
echo pg_last_error($connect);
  exit;
}

while($row = pg_fetch_array($result))
{

$coor = $row[0];
echo $coor;

}
?>

Upvotes: 3

Related Questions