PostgreSQL and PHP (on Windows)
By Rayed
After installing PostgreSQL (15 minutes job), and playing with pgAdminIII, I decided to try it with PHP.
I downloaded Apache 2 and installed it, simple wizard and it up and running.
PHP was bit harder:
- I downloaded the PHP4 zip package, it was 8Mg download. Don’t download the installer version, it had limited functionality.
- Unzip it under “C:\php”
- Move all files under “sapi”, and “dlls”, to “C:\php”.
- Copy “php.ini-dist” to “php.ini”
- Edit “php.ini” and change the following:
extension_dir = "c:\php\extensions\"
extension=php_pgsql.dll
- Add these lines to Apache configuration file (Start>Apache…>Configure..>Edit..)
LoadModule php4_module "c:/php/php4apache2.dll"
AddType application/x-httpd-php .php
PHPIniDir "C:/php"
- Restart Apache
After that I tested it with simple file “phpinfo.php”:
< ?php phpinfo(); ?>
This file should display information about your PHP installaion, make sure “postgreSQL” support is enabled, search for “pgsql”.
Fortunately I always use ADOdb for PHP data access, so I didn’t need to learn Postgres system calls, I just got sample code from MySQL, and changed thhe connection string:
<?php
include("adodb/adodb.inc.php");
$db = NewADOConnection('postgres');
$db->Connect("localhost", "user", "******", "database");
$result = $db->GetAll("SELECT * FROM articles");
if($result===false) die('DB Error:'.$db->ErrorMsg());
print_r($result);
?>
and it worked!!
Pretty easy may be I should play more with PostgreSQL.