NYoung
NYoung

Reputation: 197

How to fix mysql_connect() error in wp cli

Problem:

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

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:

Attempted solution - php.ini

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

wp--info

command. It prints the following:

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 $WP_CLI_PHP_ARGS 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 two wrappers I found on the web to create this:

#!/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' >/dev/null 2>&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" "$@"

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

Upvotes: 1

Views: 6662

Answers (4)

Andrei Stezenko
Andrei Stezenko

Reputation: 1

I'm using Windows 10, PHP 8.0.7.
Installed wp-cli via phar file.
And got this error on every command:
Call to undefined function mysql_connect()...

So I've uncommented this string in my php.ini file:

extension=mysqli

And all worked fine for me. Hope it will be helpful for someone.

Upvotes: 0

NYoung
NYoung

Reputation: 197

Right, okay.

So the issue was flagged by wp-db and it looked as if mysql wasn't working but it was mysqli. Eventually I got the php.ini file working but I thought I'd post all the various solutions that might help others. Number 4 is what worked for me.

1) Turn off mysql_connect so as to force wordpress to use mysqli_connect. Go to wp-config.php and add the line

define('WP_USE_EXT_MYSQL', false);

2) Check your php.ini file is "--with-mysqli=shared" present in the configure command box?

3) Update your mysqli. I didn't do this one, but following this, the advice seems to be to run this in your shell.

sudo apt-get install mysql-server mysql-common php7.0 php7.0-mysql

I'm running git-bash in on windows so that threw up a whole load of nonsense for me. If you're on linux this could work.

4)As above I notice php.ini wasn't give in the wp --info. I found the right file using (just create a file with that, and visit it from your server). This turned out to be in a different place than I was expecting. Then I fiddled with the bash wrapper above and eventually got this, which made the error disappear:

#!/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' >/dev/null 2>&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')
php -c $WP_CLI_PHP_ARGS "${dir}/wp-cli.phar" "$@"

If I've done something stupid let me know, thanks for your time. Feel free to ask for clarifications.

Upvotes: 1

mani
mani

Reputation: 3096

Looks like your wordpress installation is trying to use mysql_connect in wp-db.php. Wordpress defaults to using mysql_connect if it cannot find mysqli installed OR is overridden.

mysqli comes installed with php 7 (and you're using php 7).

So, check wp-config.php and confirm that WP_USE_EXT_MYSQL is defined to false

Upvotes: 0

Zoe Edwards
Zoe Edwards

Reputation: 13677

mysql_connect() is deprecated, as of PHP 5.5, and removed in PHP 7. PHP 5.5 is not a supported version of PHP, so the author should update their code.

Use mysqli_connect() instead.

Upvotes: 2

Related Questions