[英]Access denied for user ''@'localhost' (using password: NO)
I have Ubuntu 14.04 LTS. 我有Ubuntu 14.04 LTS。 I set up apache, phpmyadmin and mysql. 我设置了Apache,phpmyadmin和mysql。 I can access databases through localhost/phpmyadmin using my set password and root as username. 我可以使用设置的密码和root作为用户名通过localhost / phpmyadmin访问数据库。 I have put a link inside /etc/apache2/sites-enabled that links to the original .conf file of the website located in /etc/apache2/sites-available. 我在/ etc / apache2 / sites-enabled中放置了一个链接,该链接指向位于/ etc / apache2 / sites-available中的网站的原始.conf文件。 And it has these lines ( mypage.conf 它具有以下几行( mypage.conf
php_value mysql.default.user root
php_value mysql.default.password mypass
php_value mysql.default.host localhost
When I try to connect to database using this in my index.php it works prettily: 当我尝试使用index.php中的此数据库连接数据库时,它可以正常工作:
mysql_connect('localhost', 'root', 'mypass') or die(mysql_error());
But when I try to connect to database using this below: 但是当我尝试使用下面的方法连接到数据库时:
mysql_connect(ini_get("mysql.default.host"), ini_get("mysql.default.user"), ini_get("mysql.default.password")) or die(mysql_error());
In return it posts this error message to the page and doesn't execute the rest of the code: 作为回报,它将此错误消息发布到页面,并且不执行其余代码:
Access denied for user ''@'localhost' (using password: NO)
As I can understand from this, what it gets from default values are ''. 据我了解,默认值是''。 So I must be doing something wrong about the .conf file. 因此,我必须对.conf文件做错什么。 Can you help me about it? 你能帮我吗?
As stated by @Vanitas, I changed the connection style to PDO instead of mysql. 如@Vanitas所述,我将连接样式更改为PDO而不是mysql。 Here's my 这是我的
connection.php connection.php
<?php
DEFINE('DB_HOST', 'localhost');
DEFINE('DB_NAME', 'mydb');
DEFINE('DB_USER', 'usr');
DEFINE('DB_PASS', 'pss');
try{
$db = new PDO('mysql:host='.DB_HOST.'; dbname='.DB_NAME, DB_USER, DB_PASS);
}catch(PDOException $exeption){
echo $exeption->getMessage(). '<br/>';
die();
}
?>
And I retrive it by using 我通过使用它来检索
require_once('../../0n/connection.php');
It's outside of the web directory. 它在Web目录之外。 So, it's safe. 因此,这很安全。 I suppose. 我想。 And it's working prettily. 它正在正常工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.