title: PHP mysql_real_escape_string(): Access denied for user 'www-data'@'localhost'
body: <p>I just uploaded my website on the production server and i get the error :</p>
  
  <pre><code>Warning: mysql_real_escape_string(): Access denied for user 'www-data'@'localhost' (using password: NO) in file.php on line 106
  
  Warning: mysql_real_escape_string(): A link to the server could not be established in file.php on line 106
  </code></pre>
  
  <p>the code of the function is </p>
  
  <pre><code>include('./../inc/conn.php');
  if(isset($_GET['query']))$q = clean($_GET['query']);
  function clean($var){
      return(mysql_real_escape_string($var));
  }   
  </code></pre>
  
  <p>the code of inc/conn.php :</p>
  
  <pre><code>try {
    $dns = 'mysql:host=localhost;dbname=mydatabase';
    $user = 'root';
    $pw = 'rootpw';
  
    $options = array(
      PDO::MYSQL_ATTR_INIT_COMMAND    =&gt; "SET NAMES utf8",
      PDO::ATTR_ERRMODE =&gt; PDO::ERRMODE_EXCEPTION
    );
  
    $db = new PDO( $dns, $user, $pw, $options );
  } catch ( Exception $e ) {
      echo "Connection error : ", $e-&gt;getMessage();
      die();
  }
  </code></pre>
  
  <p>I really don't know what is going on since i have no problem on my local dev ubuntu server. Mysql, apache and php version are the same. The only thing is i use virtual host on the apache prod server. Don't know what is going on... Is there something i missed in one of the apache or php config ?</p>
  
  <p><strong>Edit</strong> 
  Here are my folder's rights:</p>
  
  <pre><code>sudo ls -l /home/user/public/domain.com/www/
  total 28
  drwxrwxr-x 13 user www-data 4096 Aug 22 12:30 adodb5
  drwxrwxr-x  2 user www-data 4096 Aug 22 12:30 ajax
  drwxrwxr-x  2 user www-data 4096 Aug 22 12:31 css
  drwxrwxr-x  9 user www-data 4096 Aug 22 12:33 gfx
  drwxrwxr-x  2 user www-data 4096 Aug 22 12:33 inc
  drwxrwxr-x  2 user www-data 4096 Aug 22 12:34 js
  </code></pre>
  
  <p>my apache virtual host config</p>
  
  <pre><code>&lt;VirtualHost *:80&gt;
    # Admin email, Server Name (domain name), and any aliases
    ServerAdmin contact@domain.com
    ServerName  www.domain.com
    ServerAlias domain.com
  
    # Index file and Document Root (where the public files are located)
    DirectoryIndex index.html index.php
    DocumentRoot /home/user/public/domain.com/www
          &lt;Directory /&gt;
                  Options FollowSymLinks
                  AllowOverride None
          &lt;/Directory&gt;
          &lt;Directory /home/user/public/domain.com/www&gt;
                  Options FollowSymLinks
                  AllowOverride All
                  Order allow,deny
                  allow from all
          &lt;/Directory&gt;
      ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
      &lt;Directory "/usr/lib/cgi-bin"&gt;
              AllowOverride None
              Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
              Order allow,deny
              Allow from all
      &lt;/Directory&gt;
  
    # Log file locations
    LogLevel warn
    ErrorLog  /home/user/public/domain.com/log/error.log
    CustomLog /home/user/public/domain.com/log/access.log combined
  &lt;/VirtualHost&gt;
  </code></pre>
  
  <p><strong>Edit 2</strong></p>
  
  <p>Ok so the problem was i didn't have any www-data user on the mysql server. So i just added user www-data with no password and no privilege in mysql and this is working fine. I will in the future trying to use PDO quote as many mentionned.
  Thanks for everyone trying to help me.</p>