CiviCRM Community Forums (archive)

*

News:

Have a question about CiviCRM?
Get it answered quickly at the new
CiviCRM Stack Exchange Q+A site

This forum was archived on 25 November 2017. Learn more.
How to get involved.
What to do if you think you've found a bug.



  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Support »
  • Using CiviCRM »
  • Using CiviMail (Moderator: Piotr Szotkowski) »
  • mysql_real_escape_string(): A link to the server could not be established
Pages: [1]

Author Topic: mysql_real_escape_string(): A link to the server could not be established  (Read 1474 times)

davej

  • Ask me questions
  • ****
  • Posts: 404
  • Karma: 21
mysql_real_escape_string(): A link to the server could not be established
February 21, 2013, 03:24:05 am
Hi,

This PHP error observed in large quantities on mailing reports on a 4.2.6 site, e.g. Mailing Detail Report
/civicrm/report/instance/28?reset=1&mailing_id_value=375&is_optout_value=1

mysql_real_escape_string(): Access denied for user ''@'localhost' (using password: NO) in .../civicrm/CRM/Mailing/BAO/Mailing.php on line 2641.
mysql_real_escape_string(): A link to the server could not be established in .../civicrm/CRM/Mailing/BAO/Mailing.php on line 2641.

The relevant code:

Code: [Select]
      $query = "
SELECT civicrm_mailing.id, civicrm_mailing.name, civicrm_mailing_job.end_date
FROM   civicrm_mailing
INNER JOIN civicrm_mailing_job ON civicrm_mailing.id = civicrm_mailing_job.mailing_id {$where}
ORDER BY civicrm_mailing.name";
      $mailing = CRM_Core_DAO::executeQuery($query);

      while ($mailing->fetch()) {
        $list[mysql_real_escape_string($mailing->id)] = "{$mailing->name} :: {$mailing->end_date}";
      }

mysql_real_escape_string() connects to db to get the current character set of the connection. "If the link identifier is not specified, the last link opened by mysql_connect() is assumed. If no such link is found, it will try to create one as if mysql_connect() was called with no arguments." Hence the "Access denied for user ''@'localhost'". I'm not clear why it's unable to find the last link opened by mysql_connect(). Possibly because CIVICRM_DSN specifies mysqli?

In any case, I don't see why mysql_real_escape_string() is needed here as the code isn't preparing a db query. And $mailing->id is an integer. Changing to addslashes() gets rid of the error.

Dave J

Hershel

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4640
  • Karma: 176
    • CiviHosting
  • CiviCRM version: Latest
  • CMS version: Mostly WordPress and Drupal
Re: mysql_real_escape_string(): A link to the server could not be established
February 21, 2013, 05:31:23 am
Quote from: davej on February 21, 2013, 03:24:05 am
In any case, I don't see why mysql_real_escape_string() is needed here as the code isn't preparing a db query. And $mailing->id is an integer. Changing to addslashes() gets rid of the error.

You may be correct.

For the time being, did you ask your host why mysql_real_escape_string fails to execute?
CiviHosting and CiviOnline -- The CiviCRM hosting experts, since 2007

See here for the official: What to do if you think you've found a bug.

Donald Lobo

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 15963
  • Karma: 470
    • CiviCRM site
  • CiviCRM version: 4.2+
  • CMS version: Drupal 7, Joomla 2.5+
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: mysql_real_escape_string(): A link to the server could not be established
February 21, 2013, 12:35:39 pm

pretty sure we dont need either mysql_real_escape_string or addslashes

so i got rid of it for 4.3 :)

lobo
A new CiviCRM Q&A resource needs YOUR help to get started. Visit our StackExchange proposed site, sign up and vote on 5 questions

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Support »
  • Using CiviCRM »
  • Using CiviMail (Moderator: Piotr Szotkowski) »
  • mysql_real_escape_string(): A link to the server could not be established

This forum was archived on 2017-11-26.