tags:

views:

1711

answers:

5

Hi all, I have a script which calls mysql_connect() to connect to a MySQL DB. When I run the script in a browser, it works as expected. However, when I run it from a command line I recieve the 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
+2  A: 

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
+5  A: 

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
Sorry, tried that but I still get the same error.
CoolGravatar
Ensure that's the path to your php.ini file.
Darryl Hein
Hmm...it would make sense that it would work, but it still doesn't.
CoolGravatar
Btw, I did use the path to my php.ini file as specified in phpinfo()
CoolGravatar
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
Does that php.ini actually exist at the path you used?
Darryl Hein
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
This is the exact command I'm using:/usr/bin/php -c /etc/php.ini -f /full/path/to/script.phpI'm also running under the same user as the other "regular" PHP script.
Darryl Hein
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
Does the php.ini have extension=mysql.so ?
Darryl Hein
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
Yes. You might not have the right php.ini file. Are you chroot'ed at all?
Darryl Hein
+1  A: 

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

Allain Lalonde
I didn't even know that file existed. I'll check that out. Thanks!
CoolGravatar
+1  A: 

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
+2  A: 

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