PHP mysql_real_escape_string():拒绝访问用户’www-data’@’localhost’

我刚刚在生产服务器上上传了我的网站,我收到错误

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

函数代码

include('./../inc/conn.PHP');
if(isset($_GET['query']))$q = clean($_GET['query']);
function clean($var){
    return(MysqL_real_escape_string($var));
}   

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();
}

我真的不知道发生了什么,因为我在我的本地开发ubuntu服务器上没有问题. MysqL,apache和PHP版本是一样的.唯一的问题是我在apache prod服务器上使用虚拟主机.不知道发生了什么…有没有我错过的apache或PHP配置?

编辑
这是我的文件夹的权利:

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

我的apache虚拟主机配置

<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>

编辑2

好吧问题是我在MysqL服务器上没有任何www-data用户.所以我刚刚添加用户www-data,没有密码,也没有MysqL的权限,这个工作正常.我将在未来尝试使用PDO引用,正如许多人提到的那样.
谢谢大家试图帮助我.

解决方法:

Note that this answer is terrible and opens you up to security vulnerabilities. It is the wrong solution. Do not do this. See further down for the actual solution to the problem. The fact that this answer got accepted just means that two people didn’t kNow what they were doing.

这是因为你的MysqL数据库中没有www数据!

您可以通过PHPmyadmin添加www-data用户并为该用户提供无权限,它应该可以正常工作

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...