4

Wanting to do a clean install of CiviCRM, I thought I should use version 4.7.21. I am using a clean install of WordPress 4.8 with no other plugins.

I get the error message below when I go to the configure screen (....<civicrm directory>/civicrm/install/index.php) and all the database checks appear red (edit: attempted installation with Drupal 7.56 with the exact same problem):

Warning: mysqli_real_escape_string() expects parameter 1 to be mysqli, null given in /home/fotgm080/public_html/temp2/wp-content/plugins/civicrm/civicrm/install/index.php on line 1282

Your database settings don't appear to be correct.

  • Does the server exist? OK (localhost)

  • Are the access credentials correct? That username/password doesn't work:

  • Can I access/create the database? I can't create new databases and the database 'fotgm080_wp685' doesn't exist. (user 'fotgm080_wp685' doesn't have CREATE DATABASE permissions.)

  • Can I access/create InnoDB tables in the database? Unable to create InnoDB tables. MySQL InnoDB support is required for CiviCRM but is either not available or not enabled in this MySQL database server. Could not determine if MySQL has InnoDB support. Assuming no.

  • Can I create temporary tables in the database? Could not login to the database.

  • Can I create lock tables in the database? Could not connect to the database server.

  • Can I create triggers in the database? Could not login to the database.

If I do a clean install of WordPress/Drupal again and try CiviCRM 4.7.20 then everything is fine.

3

This issue is corrected in the 4.7.22 release. I was able to do a clen install on a WordPress site.

  • Yes version 4.7.22 seems to resolve this and I can get past the red screen... but I've now got another problem in that the menu doesn't show after completion of the install, but this is a different problem and as it now seems to be affecting an install of version 4.7.20 it is probably something that I've started doing wrong locally! – Mick Kahn Jul 13 '17 at 21:53
2

I had the same error in Drupal 7.56:

mysqli_real_escape_string() expects parameter 1 to be mysqli, null given ...

Exactly on line 1282 of the new civicrm/install/index.php, which is different from CiviCRM 4.7.20's index.php. So I just replaced the new file by the old one and all the checks became green and the installation proceeded normally (Drupal and CiviCRM share the same DB). CiviCRM is working fine.

Edit: after investigating...

I compared both versions install/index.php files and there is actually a new function that replaces all the connections to MySQL that were made previously and directly through mysqli_connect($server, $username, $password).

This is the new function:

protected function connect($host, $username, $password, $database = '') {
  $hostParts = explode(':', $host);
  if (count($hostParts) > 1 && strrpos($host, ']') !== strlen($host) - 1) {
    $port = array_pop($hostParts);
    $host = implode(':', $hostParts);
  }
  else {
    $port = '';
  }
  $conn = @mysqli_connect($host, $username, $password, $database, $port);
  return $conn;
}

This function does not add or alter procedures in the installation process. It is basically a verification and treatment of the $server variable passed as parameter ($host). What it does:

  1. Does the $server/$host string contains a port?
    • if YES than:
      1. pop it out from the string and place it under the $port variable;
      2. replace the original $host string by a new string without the port.
    • if NO than:
      1. assign an empty string to $port.
  2. Establish a connection with the "new" separated parameters like so:

    @mysqli_connect($host, $username, $password, $database, $port)
    

    If you notice, the difference from the old version is just one: 2 more parameters provided to mysqli_connect, in order to support websites running in different ports.

Which means, for all connections without a specific port (most cases), replacing index.php of version 4.7.21 with the one from version 4.7.20 will work (here's a pastebin with the old index.php).

How to fix it in version 4.7.21

The problem does not lie in the connect function parameters treatment, but in the $port value, which if it becomes an empty string mysqli's result will be null. Also the $database value is always empty.

These can be fixed either by:

  1. adding the proper value to $database when connect is invoked
  2. providing a valid value for $port (not an empty string)

Quickfix (2.)

Do not allow the $port to become an empty string, i.e. replace only this line in the install/index.php@connect:

$port = '';

By this line:

$port = NULL;

This will prevent mysqli_connect result to be null.

I have tested this in a new installation and it works.

Proper fix (1.):

For making use of the $database parameter, these lines:

$this->conn = $this->connect($server, $username, $password);

Must be replaced with:

$this->conn = $this->connect($server, $username, $password, $database);

These include lines 1040, 1075, 1105, 1145, 1226, 1270 and 1409.

For the remaining lines (978 and 1192 where the connect is invoked), since there is no available database name value, we need to add to the respective functions the $database parameter, i.e.:

  1. For requiredMysqlConnection:
    • replace with public function requireMysqlConnection($server, $username, $password, $database, $testDetails) {
    • replace with $this->conn = $this->connect($server, $username, $password, $database);
    • line 445 (after $databaseConfig['password'],) add a new line with $databaseConfig['database'], ($dbName should not be used since it only contains a default value like 'CiviCRM')
  2. For requireMySQLAutoIncrementIncrementOne:
    • replace with public function requireMySQLAutoIncrementIncrementOne($server, $username, $password, $database, $testDetails) {
    • replace with $conn = $this->connect($server, $username, $password, $database);
    • line 465 (after $databaseConfig['password'],) add a new line with $databaseConfig['database'], ($dbName should not be used since it only contains a default value like 'CiviCRM')

This handles empty $database values. However, if $port is actually an empty string, mysqli_connect will also return null. Therefore, the line mentioned in the previous Quickfix (2.) should also be changed.

I have tested this fix and it worked as well. If you have any suggestions/improvements, please comment below and I'll edit.

  • Thanks for the confirmation that it isn't me. Will stick with 4.7.20 and hope that someone knowledgeable will explain and fix it. – Mick Kahn Jul 9 '17 at 20:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.