PHP command line mysql_connect () Error

I am running a php page from the command line to update mysql db after the php exec command completes. This is working fine.

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

Fatal error: Call to undefined function mysql_connect() 

The script works fine in the browser, which means mysql is set to OK. How to enable mysql launch in php from command line?

Many thanks.

+6
command-line php mysql
source share
4 answers

First of all, you can use

 php -m 

from the command line to check which extensions are included.

If mysql is not listed, it means it is not enabled / not loaded - which is probably the case here.


Then you will want to use

 php --ini 

to check which .ini / files are read by PHP.


Once you know which php.ini used, you will have to edit it and add something like this:

 ; configuration for php MySQL module extension=mysql.so 

To download the mysql extension.


Depending on your distribution, the file may be .ini for the extension (for example, Ubuntu does this).

If so, you can also create a new .ini file ( mysql.ini for example) next to the others, and put the two lines that I placed in this new file.

+11
source share

Check the path to the PHP executable. You did not indicate which OS it is in, but, for example, in OSX only running php executes the built-in PHP interpreter. If you use MAMP, you need to run /Applications/MAMP/bin/php5.2/bin/php (or any other way) to simulate what works in your browser.

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

+4
source share

I had a similar problem recently (maybe for you the same problem):

I have indicated my preferred php.ini in the Apache configuration. However, this does not explicitly affect PHP in the CLI. Check which php.ini used by php: php --ini . Either modify it, or select a different configuration file by changing some of them:

php.ini searches in these places (in order):

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

  • 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 considered in the following order: [HKEY_LOCAL_MACHINE \ SOFTWARE \ PHP \ xyz], [HKEY_LOCAL_MACHINE \ SOFTWARE \ PHP \ xy] and [HKEY_LOCAL_MACHINE \ SOFTWARE \ PHP \ x], where x, y and z mean major PHP, minor and release versions. If these keys have a value for IniFilePath, then the first one will be used as the location of php.ini (for Windows only).

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

  • Current working directory (except CLI)

  • Web server directory (for SAPI modules) or PHP directory (otherwise on Windows)

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

I just changed the PHPRC environment variable to my preferred php.ini location.

0
source share

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

Despite the fact that "php -ini" indicated that it is processing the correct .ini file, this is not the case, so I had to force it into the script title bar.

0
source share

All Articles