tags:

views:

360

answers:

5

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".

A: 

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.

Amber
there is no apache folder in the path /var/log/
Neveen
A: 

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

graham.reeds
how can i do this ??
Neveen
that is my codefunction 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
Neveen
Nate shows this in his answer.
graham.reeds
Added your code to the question.
graham.reeds
+3  A: 

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.

Nate
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
Neveen
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()`.
Nate
A: 

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.

Residuum
the postgresSql plus installed into my machine and the same error is appeared. i dont know why??
Neveen
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?
Residuum
+1  A: 

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
krishna