Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

How can I use php to connect to a postgresql table

How can I use php to connect to a postgresql table

How can I use php to connect to a postgresql table

Dear All,

Please how can I use PHP to connect to a postgresql database/table and insert/delete data or retrieve data.Is it better to use ASP? Can ASP work in Linux environment?

Any help is appreciated.

RE: How can I use php to connect to a postgresql table


$dbconn = pg_connect("host=IP ADDRESS dbname=DBNAME user=USERNAME password=PASSWORD")
    or die('Could not connect: ' . pg_last_error());

$query = "SELECT field1 from table";

$result = pg_query($query) or die('Query failed: ' . pg_last_error());
    $i = 0;
      while ($i <$num) {
        while ($line = pg_fetch_array($result)) {
            $field1 = $line['field1'];
$i = $i + 1;



RE: How can I use php to connect to a postgresql table

Dear All,

Thanks MercJones, I have used the code you sent to customise it according to my details but Please  how can I run the PHP program to connect to a PostgreSQL database and select contents of a table and print it on the browser.  I could not run the program. I don't know how to run it or perhaps how to activate the php. I have done some configurations on httpd.conf and php.ini files after installing php. See the program below:

// Connecting, selecting database
$dbconn = pg_connect("host= dbname=rftDatabase user=globus password=globususer");

// Performing SQL query
$query = 'SELECT * FROM Criteria_Parameters';
$result = pg_query($query);

// Printing results in HTML
echo "<table>\n";
while ($line = pg_fetch_array($result, null, PGSQL_ASSOC)) {
    echo "\t<tr>\n";
    foreach ($line as $col_value) {
        echo "\t\t<td>$col_value</td>\n";
    echo "\t</tr>\n";
echo "</table>\n";

// Free resultset

// Closing connection

Is it possible to put the code int a function and then execute the function on a click of a button?

Any help is appreciated

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close