Posted on October 15th, 2011 No comments
This is a recurring problem i had. For some reason (which i later found out), i couldnt get my php scripts to connect to postgresql. The scripts borked at, for example, pg_connect() (unable to find the function, blah blah).
First off, usually php doesnt come configured to run the postgresql dll out of the box (the windows extension is commented), so you have to uncomment it yourselves.
In php.ini, uncomment (remove the “;”) the following line:
so it becomes
Still, when i restarted Apache, i got the error:
“Unable to load dynamic library ‘c:\Programs\PHP\ext\php_pgsql.dll’”
Which was intriguing since the dll was where it was supposed to be.
As it turns out, on later versions of php, the dll is linked to “libpq.dll”, a postgresql dll. So, to fix things, i added the following line to httpd.conf:
(replace the path to your postgresql installation dir, of course – and be careful copying the line above because of the “)
this way the dll is preloaded when apache is started and we dont get the “Unable to link…” error.
This was tested with xampp on windows xp pro, with apache 2.2.17, php 5.3.5 and postgresql 9.0. Other versions can probably affect the solution presented.
PS – On some situations (probably php versions), it is needed to replace the php_pgsql.dll file that comes with php with one from a previous version. This wasnt necessary on my case.
Heres a useful link from where i got the info:
http://php.net/manual/pt_BR/install.windows.apache2.php (see the comments)
Ran into the same problem, but this time on windows 7 64bit. I was using XAMPP 1.7.7 (Apache 2.2.21 / PHP 5.3.8) and postgresql 9.1 64bit.
- I uncommented the php_pgsql.dll in php.ini, restarted apache and got an error.
- Added the loadfile line (see above) to httpd.conf and the apache service couldnt start.
No matter what i did, nothing worked. After a few hours of head banging, i had the bright idea of searching for the 64bit part on the postgresql install and i found this article:
(in case you dont want to read, it says, in a nutshell: you are safe using the 32bit version of postgresql on a 64bit OS)
So i thought the problem might be from just that. Heres what i did:
- Installed the 32bit version of postgresql 9.1
- Added the loadfile line to httpd.conf.
- Restarted the apache service…and it worked.
So we end up with: Windows 7 Ultimate 64bit + XAMPP 1.7.7 + PostgreSQL 9.1 32bit + Smile
Posted on October 6th, 2008 No comments
Before i came up with this solution, i had some trouble formatting and padding decimal numbers, specifically when i want to do it before and after the decimal mark. To top it all, i wanted the result signed (with a plus sign(+) or minus sign(-) depending on the value of the variable)
I want this “-4.3″ into this “-04.3″
Or this “6″ into this “+06.0″
PHP has a lot of number formatting options, from number_format, to sprontf and printf. Well, just search the php docs and you will see what i mean.
The thing is that, as always, i wanted to do it in as less code as possible so, to do what i mentioned above, and after a bit of testing, i came up with this:
$value = -14.6;
printf('%1$s%2$02d.%3$01s', ($value <0 ) ? '-' : '+',floor(abs($value)),(abs($value)-floor(abs($value)))*10);
Although pretty simple, i think its still a bit contrived and dirty. But it works.
A bit of background:
the first part of the printf means:
%1 will contain the first parameter (the value after the first comma), with $s as the format (in sprintf, ‘s’ means string, so it will just output the result of the first parameter, no formatting)
%2 has $02d as format so it will show a pad of two digits of an integer treated number showed as decimal
%3 has $01s as format – like i mentioned for %1, shows a string with a pad of one digit
Since i want to keep these posts simple, to serve as reference and jump to other reference sites, i will just leave some links:
PHP docs for sprintf (it has all the formatting…formats):
Just ask if you need any more feedback or help.
Posted on September 24th, 2008 No comments
This site will contain an archive of all the junk, problems, code snippets, tricks, hacks, solutions and garbage i want to keep to later use, reuse, alter, etc.
Hopefully it will be of help to someone else.
Este site conterá um arquivo de todas as tralhas, problemas, soluções e lixo que eu vou querendo arquivar para mais tarde recordar.
Talvez ajude mais alguém.