我刚刚将我的网站上传到生产服务器上,但出现错误:
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 用户。所以我只是在mysql中添加了没有密码且没有权限的用户www-data,这工作正常。我将在未来尝试使用许多提到的 PDO 引用。感谢所有试图帮助我的人。