-1

Using engine LiteDiary and have problem with search on site: if user not logged on and trying to search something, this error popped out:

Warning: mysql_real_escape_string(): Access denied for user 'apache'@'localhost' (using password: NO) in /var/www/bmz/data/www/info.mobilux.info/sources/lib/drivers/mysql/mysql.php on line 89

Warning: mysql_real_escape_string(): A link to the server could not be established in /var/www/bmz/data/www/info.mobilux.info/sources/lib/drivers/mysql/mysql.php on line 89

Here code:

<?
class DB_Connection {
    public $driver,$server,$user,$database,$prefix,$connection,$queries,$encoding,$log;
    private $password;

    function __construct(array $a,$enc=array('enc'=>'utf8','coll'=>'utf8_unicode_ci'),$l=false,$cache=true) {
        $this->server=$a['server'];
        $this->user=$a['user'];
        $this->password=$a['pass'];
        $this->name=$a['db'];
        $this->prefix=$a['prefix'];
        $this->queries=0;
        $this->log=$l?array():false;
        $this->cache=$cache;
        $this->connected=false;
        $this->encoding=$enc;
        $this->set_enc=false;
        $this->driver='mysql';
    }
    function init() {
        if (!$this->connected) {
            if ($this->connection=mysql_connect($this->server,$this->user,$this->password)) {
                mysql_select_db($this->name,$this->connection);
                $this->connected=true;
                if (!$this->set_enc) $this->set_enc=self::q("SET NAMES '{$this->encoding['enc']}' COLLATE '{$this->encoding['coll']}'");
            }
        }
    }
    function q($q,$p=true) {
        self::init();
        $this->queries++;
        if ($this->log!==false) $this->log[$this->queries]=$q;
        if ($p) $q=str_replace('`_',"`{$this->prefix}_",$q);
        if ($this->cache&&(strpos($q,'UPDATE')!==false||strpos($q,'INSERT')!==false||strpos($q,'DELETE')!==false)) self::cache_clean();
        return mysql_query($q,$this->connection);
    }
    function qncl($q,$p=true) {
        self::init();
        $this->queries++;
        if ($this->log!==false) $this->log[$this->queries]=$q;
        if ($p) $q=str_replace('`_',"`{$this->prefix}_",$q);
        return mysql_query($q,$this->connection);
    }
    function qm($q,$r=false) {
        foreach ($q as $k=>$i) if (!self::q($i)) {$r[]=array('no'=>mysql_errno(),'text'=>mysql_error());}
        return $r;
    }
    function query($query,$one=false,$cache=true) {
        if ($this->cache&&$cache&&file_exists(CACHE.'sql/'.md5($query))) {
            $b=unserialize(file_get_contents(CACHE.'sql/'.md5($query)));
        } else {
            $q=self::q($query);
            if ($q) {
                if (!$one) {
                    for ($b=array();@$c=mysql_fetch_assoc($q);$b[]=$c);
                } else {
                    $b=mysql_fetch_assoc($q);
                }
                if ($this->cache&&$cache) self::cache_query($query,$b);
            } else {
                return false;
            }
        }
        if ($b) return $b;
    }
    function qq($q) {
    return @mysql_result(self::q($q),0,0);
    }
    function last_id() {
    return mysql_insert_id();
    }
    function cache_query($q,$r) {
        if ($f=fopen(SRC.'cache/sql/'.md5($q),'ab')) {
            fwrite($f,serialize($r));
            fclose($f);
        } else {echo 'Cannot open file for caching';}
    }
    function cache_clean() {
        if (opendir($d=CACHE.'sql')) {
            while ($f=readdir()) {
                if ($f!='.'&&$f!='..'&&is_file($n=$d.'/'.$f)) {
                    unlink($n); 
                }
            }
            closedir();
        }
    }
    function esc($a) {
    return  mysql_real_escape_string($a);
    }
}?>

I hope it's simple problem and someone help me. Thanks!

Andrii
  • 19
  • 2
  • 9

1 Answers1

0

One may wonder why one needs to be logged in to use this function. Here is the answer.

Edit: Your solution will probably involve connecting to the mysql database, but without performing any sort of user credential check so that the msyql connection is active but the user does not need to 'log in' per se.

Community
  • 1
  • 1
SyntaxTerror
  • 346
  • 1
  • 10
  • To get the correct charset/encoding settings that get tracked for the connection. As is explained in the accepted answer of your link – nl-x Jul 01 '14 at 19:09