Command Line PHP mysql_connect() Error

2019-02-22 02:29发布

I am running a php page form the command line to update a mysql db once a php exec command has completed. This has been working OK.

However, I have just reinstalled my server and now the script is returning the following error when I run it on the command line:

Fatal error: Call to undefined function mysql_connect() 

The script runs fine in the browser which means mysql is installed OK. How do I enable mysql in PHP to be run from the command line?

Many thanks.

4条回答
对你真心纯属浪费
2楼-- · 2019-02-22 03:24

I had a similar problem a short time ago (maybe it's the same problem for you):

I had specified my prefered php.ini in Apache's config. However, this obviously doesn't affect php in CLI. Check which php.ini is used by php: php --ini. Either modify it or choose a different config file by changing some of these:

php.ini is searched in these locations (in order):

  • SAPI module specific location (PHPIniDir directive in Apache 2, -c command line option in CGI and CLI, php_ini parameter in NSAPI, PHP_INI_PATH environment variable in THTTPD)

  • The PHPRC environment variable. Before PHP 5.2.0 this was checked after the registry key mentioned below.

  • As of PHP 5.2.0, the location of the php.ini file can be set for different versions of PHP. The following registry keys are examined in order: [HKEY_LOCAL_MACHINE\SOFTWARE\PHP\x.y.z], [HKEY_LOCAL_MACHINE\SOFTWARE\PHP\x.y] and [HKEY_LOCAL_MACHINE\SOFTWARE\PHP\x], where x, y and z mean the PHP major, minor and release versions. If there is a value for IniFilePath in these keys, then the first one found will be used as the location of the php.ini (Windows only).

  • [HKEY_LOCAL_MACHINE\SOFTWARE\PHP], value of IniFilePath (Windows only).

  • Current working directory (except CLI)

  • The web server's directory (for SAPI modules), or directory of PHP (otherwise in Windows)

  • Windows directory (C:\windows or C:\winnt) (for Windows), or --with-config-file-path compile time option

I've just modified the PHPRC environment variable to my prefered location of php.ini.

查看更多
对你真心纯属浪费
3楼-- · 2019-02-22 03:29

Check the path to your PHP executable. You didn't mention which OS this is under, but, for example, under OSX just running php executes the built-in PHP interpreter. If you're using MAMP you'll need to run /Applications/MAMP/bin/php5.2/bin/php (or whatever your path is) to imitate what's running in your browser.

Alter the path to your web server's php accordingly (based on AMP installation, OS, etc.).

查看更多
Animai°情兽
4楼-- · 2019-02-22 03:30

For me the answer was to change the first line in my script from '#!/usr/bin/php' to '#!/usr/bin/php -c /etc/php.ini'.

Even though 'php --ini' indicated it was processing the correct .ini file, it wasn't, so I had to force it on the header line of the script.

查看更多
仙女界的扛把子
5楼-- · 2019-02-22 03:33

First of all, you can use

php -m

from the command-line, to check which extensions are enabled.

If mysql is not in the list, it means it's not enabled / loaded -- which is probably the case, here.


Then, you'll want to use

php --ini

to check which .ini file(s) is/are read by PHP.


Once you've found out which php.ini file is used, you'll have to edit it, and add something like this :

; configuration for php MySQL module
extension=mysql.so

To load the mysql extension.


Depending on your distribution, the might be a .ini file per extension (Ubuntu does that, for instance).

If that's the case, you could also create a new .ini file (mysql.ini for instance) next to the other ones, and put the two lines I posted into that new file.

查看更多
登录 后发表回答