I have a script which calls mysql_connect()
to connect to a MySQL DB. When I run the script in a browser, it works. However, when I run it from a command line
php-cli.ini might be the file your command line php interpreter is using.
Please make sure this line appears in php.ini
extension=php_mysql.dll
Please note that the php.ini used in command line may be different from the php.ini used by apache.
Check if you php cmd version actually has mysql support:
<? php echo php_info() ?>
If not, then probably it's because it is a different version than the one used by your web server.
Run php using the "-m" parameter in order to list all the compiled modules:
$ php -m
You should see the "mysql" module. If not, then i guess the php cmd version has not been compiled using the --with-mysql or the "configure" script could not included it due some reason.
While it may be a rudimentary answer, make sure you have the most up to date PHP client, and make sure that it's looking at the same PHP folder that you're working out of through Apache.
If that doesn't work, try playing with mysqli and see if it's globally for the entire MySQL portion of PHP. If mysqli works and mysql_connect() doesn't, well, then, that's as good a time as any to swtich over to the OO side :-)
This problem can arise when you are running Mac OS X and MAMP. The command line version tries to use the MAC OS X version, which happens to load no php.ini by default.
You can check that with the command:
php --ini
See the missing config file?
Configuration File (php.ini) Path: /etc
Loaded Configuration File: !!! THE CONFIG FILE IS MISSING HERE !!!
Scan for additional .ini files in: /Library/Server/Web/Config/php
Additional .ini files parsed: (none)
What you have to do is, load up the php.ini of your choice and copy it to the default location of your command line interpreter which is /etc and then run the above command again, you should see that the php.ini file is loaded which should look like:
Configuration File (php.ini) Path: /etc
Loaded Configuration File: /etc/php.ini
Scan for additional .ini files in: /Library/Server/Web/Config/php
Additional .ini files parsed: (none)
You should also add this to the PHP.ini file in /etc
mysql.default_socket = /Applications/MAMP/tmp/mysql/mysql.sock
This should fix every problem.
phew..
My system is CentOS. In my case, the the path to mysql.so is incorrect.
When I type
php -m
There is no mysql.
In my original /etc/php.d/mysql.ini there is only one line:
extension=mysql.so
So I use command:
rpm -ql php-mysql | grep mysql.so
to find out the correct path, which is
/usr/lib64/php/modules/mysql.so
then I replaced the line to :
extension=/usr/lib64/php/modules/mysql.so
then
service httpd restart
and
php -m
mysql is now loaded.