Sounds like the same problem I get on my box, but fortunately I know why (I just can't be bothered to fix it!). The likely problem is that you are running PHP as mod_php under Apache and using the libphp4.so loaded when apache starts.
Whereas, when running in a shell, you are obviously running the php binary.
These are not necessarily the same versions of php (my shell one is 4.2.2, but the module is 4.1.2 inline with my ISP) and the bin executable was probably not configured with the mysql library, while the .so was. To test this, try grabbing the output from php when run on the command line
php -i > info.html
this will dump the results of a phpinfo call into the file info.html
then - run a script through the web server with a phpinfo call in it
compare the results and see what you get.
The solution should be relatively simple - just find the binary that was created (if it was created) when the library was made and replace the current php binary
which php
will tell you where the current php binary is located -its up to you to find the other.
If you have no replacement php binary then you'll need to recompile the source to create one with all the right bits and pieces configure.