I just uploaded my website to a production server and I got an error:
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
function code
include('./../inc/conn.php'); if(isset($_GET['query']))$q = clean($_GET['query']); function clean($var){ return(mysql_real_escape_string($var)); }
code inc / conn.php:
try { $dns = 'mysql:host=localhost;dbname=mydatabase'; $user = 'root'; $pw = 'rootpw'; $options = array( PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8", PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION ); $db = new PDO( $dns, $user, $pw, $options ); } catch ( Exception $e ) { echo "Connection error : ", $e->getMessage(); die(); }
I really don't know what is going on since I have no problem on my local ubuntu dev server. The version of Mysql, apache and php are the same. The only thing I use is a virtual host on the apache proxy server. I donβt know what is happening ... Is there something I missed in one of the apache or php configurations?
Edit Here are my rights to the folder:
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
my apache virtual host configuration
<VirtualHost *:80> # 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 <Directory /> Options FollowSymLinks AllowOverride None </Directory> <Directory /home/user/public/domain.com/www> Options FollowSymLinks AllowOverride All Order allow,deny allow from all </Directory> ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/ <Directory "/usr/lib/cgi-bin"> AllowOverride None Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch Order allow,deny Allow from all </Directory> # Log file locations LogLevel warn ErrorLog /home/user/public/domain.com/log/error.log CustomLog /home/user/public/domain.com/log/access.log combined </VirtualHost>
Edit 2
Ok, so the problem was that I did not have www data users on mysql server. So I just added user www data without password and no privileges in mysql, and it works fine. I will be trying to use a PDO quote in the future, as many mention. Thanks to everyone who is trying to help me.