How to fix mysql_connect() error in wp cli

admin

Administrator
Staff member
Problem:

I am trying to use wp cli to do stuff. As an example update wordpress:

Code:
wp core update

Fatal error: Uncaught Error: Call to undefined function mysql_connect() in 
path\to\wp-includes\wp-db.php:1564
Stack trace:
#0 path\to\wp-includes\wp-db.php(592): wpdb->db_connect()
#1 path\to\wp-includes\load.php(404): 
wpdb->__construct(details)
#2 path\to\public\wp-settings.php(106): require_wp_db()
#3 phar://path/to/wp-cli.phar/php/WP_CLI/Runner.php(1182): require('C:\\path\\to\\...')
#4 phar://path/to/wp-cli.phar/php/WP_CLI/Runner.php(1107): WP_CLI\Runner->load_wordpress()
#5 phar://path/to/wp-cli.phar/php/WP_CLI/Bootstrap/LaunchRunner.php(23): WP_CLI\Runner->start()
#6 phar://path/to/wp-cli.phar/php/bootstrap.php(75): WP_CLI\Bootstrap\LaunchRunner->process(Object(WP_CLI\Bootstrap\BootstrapState))
#7 phar://path/to/wp-cli.phar/php/wp-cli.php(23): WP_CLI\bootstrap()
#8 phar://C:/ in path/to\wp-includes\wp-db.php on line 1564

As far as I can see the error is in mysql_connect().

I have read through the following answers:

<ul>
<li><a href="https://stackoverflow.com/questions/13825108/undefined-function-mysql-connect">Undefined function mysql_connect()</a> - This seemed to suggest downloading some packages. I am reticent to do this cos I don't understand what they do (and currently I'm running php using MAMP so I am not sure if this will cause me more problems) but this did suggest to me that the issue was with php.ini which informs an attempted solution below.</li>
<li><a href="https://stackoverflow.com/questions/10615436/fatal-error-call-to-undefined-function-mysql-connect">Fatal error: Call to undefined function mysql_connect()</a> - I don't think there is an error in my login details (the site itself works) so this doesn't <em>seem</em> to be the problem</li>
</ul>

Attempted solution - php.ini

When I check which php.ini wp cli is using via the

Code:
wp--info

command. It prints the following:

Code:
OS:     Windows NT 10.0 build 17134 (Windows 10) i586
Shell:  C:\Program Files\Git\usr\bin\bash.exe
PHP binary:     C:\MAMP\bin\php\php7.2.1\php.exe
PHP version:    7.2.1
php.ini used:
WP-CLI root dir:        phar://wp-cli.phar
WP-CLI vendor dir:      phar://wp-cli.phar/vendor
WP_CLI phar path:       C:\path\to\public
WP-CLI packages dir:
WP-CLI global config:
WP-CLI project config:
WP-CLI version: 1.5.1

So it seems no php.ini has been used here. So I think I need to fix that. In order to do that I've found <a href="https://make.wordpress.org/cli/handbook/config/#environment-variables" rel="nofollow noreferrer">$WP_CLI_PHP_ARGS</a> which I'm trying to put in. Now I'm no coding superstar, but it seems I need to build a bash script to act as a wrapper because they don't work in the .phar version so I have combined <a href="https://github.com/wp-cli/wp-cli/issues/956#issuecomment-32097406" rel="nofollow noreferrer">two</a> wrappers I found on the web to create this:

Code:
#!/usr/bin/env sh

dir=$(d=${0%[/\\]*}; cd "$d"; pwd)

# See if we are running in Cygwin by checking for cygpath program
if command -v 'cygpath' &gt;/dev/null 2&gt;&amp;1; then
   # Cygwin paths start with /cygdrive/ which will break windows PHP,
   # so we need to translate the dir path to windows format. However
   # we could be using cygwin PHP which does not require this, so we
   # test if the path to PHP starts with /cygdrive/ rather than /usr/bin
   if [[ $(which php) == /cygdrive/* ]]; then
       dir=$(cygpath -m $dir);
   fi
fi

dir=$(echo $dir | sed 's/ /\ /g')
"${WP_CLI_PHP}" $WP_CLI_PHP_ARGS "${dir}/wp-cli.phar" "[email protected]"

When I run this it variously complains. I imagine I have made some kind of basic error. (I've also put "export WP_CLI_PHP_ARGS=/C/MAMP/bin/php/php7.2.1/php.ini-production" in my .bash_profile).