11

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 I receive the following error:

Call to undefined function mysql_connect()

This seems completely paradoxical. Anyone have any ideas as how I can run it from the command line. Btw, I run it from a bash shell like so:

php /path/to/script.php
hakre
  • 193,403
  • 52
  • 435
  • 836
CoolGravatar
  • 5,408
  • 7
  • 35
  • 42

7 Answers7

11

It maybe using a default PHP configuration. I have found before that it doesn't use the same php.ini or doesn't use one at all. Therefore some of the extensions won't be enabled.

Do this instead:

php -c /etc/php.ini /path/to/script.php

Where /etc/php.ini is the path to your ini file. You can find this by doing a phpinfo();

Darryl Hein
  • 142,451
  • 95
  • 218
  • 261
  • Ensure that's the path to your php.ini file. – Darryl Hein Oct 08 '08 at 03:48
  • Hmm...it would make sense that it would work, but it still doesn't. – CoolGravatar Oct 08 '08 at 03:53
  • Btw, I did use the path to my php.ini file as specified in phpinfo() – CoolGravatar Oct 08 '08 at 03:55
  • Did you check if the php.ini you are refering to has the MySQL extension loaded. You might always trying running a phpinfo() from the command line. It will be hard to read, but will tell if you MySQL is load and if it's using the php.ini you told it to. – Darryl Hein Oct 08 '08 at 03:55
  • Does that php.ini actually exist at the path you used? – Darryl Hein Oct 08 '08 at 03:57
  • When I run phpinfo() it indicates that the extension is loaded. But when I run the command php -i it indicates that I dont. Maybe I have a whole different issue on my hands. – CoolGravatar Oct 08 '08 at 04:01
  • This is the exact command I'm using: /usr/bin/php -c /etc/php.ini -f /full/path/to/script.php I'm also running under the same user as the other "regular" PHP script. – Darryl Hein Oct 08 '08 at 04:05
  • I've run that exact command, except with my php.ini file specified in phpinfo() and the full path to my file, but it still doesn't work. I receive the same error. Also, FYI I'm able to login to MySQL fine via command line – CoolGravatar Oct 08 '08 at 04:14
  • Does the php.ini have extension=mysql.so ? – Darryl Hein Oct 08 '08 at 04:19
  • I couldn't find it in my php.ini. But phpinfo() indicates that there is a MySQL module. Wouldn't this be the result of having extension=mysql.so in my php.ini? – CoolGravatar Oct 08 '08 at 04:24
  • Yes. You might not have the right php.ini file. Are you chroot'ed at all? – Darryl Hein Oct 08 '08 at 04:53
3

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.

Gravstar
  • 1,071
  • 6
  • 10
2

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 :-)

Robert Elwell
  • 6,598
  • 1
  • 28
  • 32
1

php-cli.ini might be the file your command line php interpreter is using.

Allain Lalonde
  • 91,574
  • 70
  • 187
  • 238
1

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.

boxoft
  • 357
  • 2
  • 10
  • In my case, that line does appear (in the php.ini which `phpinfo()` refers to as `Loaded Configuration File`), but mysql_connect doesn't work. To make it work, I've had to change that line to `extension=ext\php_mysql.dll` – Shawn Jun 27 '12 at 15:38
1

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

Herr
  • 2,725
  • 3
  • 30
  • 36
  • The update to the php.ini for the `mysql.default_socket` entry (`mysql.default_socket = /Applications/MAMP/tmp/mysql/mysql.sock`) effectively solves the problem on Mac OS, using MAMP. – wiredolphin Sep 05 '19 at 13:56
0

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.

Dubbo
  • 686
  • 6
  • 8