0

i wanna to connect to postgresql database using php but it display a blank screen when submit and doesnt execute the query

Thanks in advance

Update: this is my code

function execute_query($dbName,$query){ 
  $host = "localhost"; 
  $user = "postgres"; 
  $pass = ""; 
  $db = "test"; 
  echo "before create connection"; 
  $con = pg_connect ("host=$host dbname=$db user=$user password=$pass"); 
  echo "After connection is created"; 
  if (!$con) { 
    echo "not connected"; 
//   die('Could not connect: ' . mysql_error());
  } 
  $result = pg_query($con, $query); 
  pg_close($con); 
  return $result; 
}

The output: display the message "before connection" but doesn't display the message "After connection is created" or "not connected".

2
  • 1
    You really need to flesh out the question if you expect a reasonable answer. Commented Aug 24, 2009 at 13:17
  • I'm sorry, but that's not enough information. Please look at the php and/or apache log for errors or perhaps turn on display for errors in php.ini. Commented Aug 24, 2009 at 13:19

5 Answers 5

7

The problem is likely a PHP error that’s getting recorded to a log file somewhere. Locate the log file, or enable showing the log errors on your page by using the following at the top of your script:

ini_set('display_errors', '1');
error_reporting(E_ALL | E_STRICT);

This is a short-term solution and can’t be used in deployment (where you want to set display_errors to 0). For a long-term solution, you really want to locate the Apache or PHP error log and tail it.

To try to find the error log, run the following script:

<?php phpinfo(); ?>

In the Configuration > PHP Core section, look for error_log. If that’s not set, you can set it in your php.ini file. All errors will be recorded to that file, even if you have display_errors set to 0.

Sign up to request clarification or add additional context in comments.

2 Comments

i used the previous script and it works. but it display the error "Fatal error: Call to undefined function pg_connect()". Thanks for ur help
It sounds like PostgreSQL support is not installed. Use the <?php phpinfo(); ?> script and verify that there is a section titled pgsql, which is the PHP module that includes the pg_connect() function. You may also want to look for a section titled pdo_pgsql, which is a more modern interface for PostgreSQL. If pdo_pgsql is present, you could use the PDO() constructor and related functions instead of pg_connect().
2

Add a php file to your server and put this in that file:

<?php
echo phpinfo();
?>

When you open that file from the browser, check if postgres support is setup for php. you should something like this on the page:

pgsql

PostgreSQL Support  enabled
PostgreSQL(libpq) Version   8.2.3
Multibyte character support enabled
SSL support disabled
Active Persistent Links 0
Active Links    0

Comments

0

Try checking your web server's error log (for instance, /var/log/apache2/error.log is a common location for the Apache2 log) and see if there is an error reported from PHP there.

1 Comment

there is no apache folder in the path /var/log/
0

You may want to set the php error reporting level in your ini file if this is a local development server.

1 Comment

that is my code function execute_query($dbName,$query){ $host = "localhost"; $user = "postgres"; $pass = ""; $db = "test"; echo "before create connection"; $con = pg_connect ("host=$host dbname=$db user=$user password=$pass"); echo "After connection is created"; if (!$con) { echo "not connected"; // die('Could not connect: ' . mysql_error()); } $result = pg_query($con, $query); pg_close($con); return $result; } The output: display the message "before connection" but doesn't display the message "After connection is created" or "not connected". Thanks
0

It seems, that your php does not have postgresql support, cf. http://us.php.net/manual/en/ref.pgsql.php:

Note: Not all functions are supported by all builds. It depends on your libpq (The PostgreSQL C client library) version and how libpq is compiled. If PHP PostgreSQL extensions are missing, then it is because your libpq version does not support them.

2 Comments

the postgresSql plus installed into my machine and the same error is appeared. i dont know why??
What kind of machine do you use? Windows / Linux / Mac? If Linux, what distribution? Have you installed all dependencies for using PHP with Postgresql, especially the right version of libpq?

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.