您的位置:首页 > 其它

常出现的Access denied for user 'root'@'localhost' (using password: YES)及其相关问题解决

2015-02-03 17:07 609 查看
/**

*author:田元

*

*company:whu eis

*contact:tianyuan168326@outlook.com

*

**/

从重装数据库开始:

软件版本:php 5.5.0 /mysql 5.0.1

安装平台:微软web程序安装平台

问题所在

php code

{

$sqliconnect=new mysqli("localhost","root","52ziji","test");

$query_result=$sqliconnect->prepare("select * from currentorder");

}

“Connect failed: Access denied for user 'root'@'localhost' (using password: YES)” from php function

解决方法:<1>猜测为权限不足,经过stackoverflow,大体为一个解决方法(eg:https://stackoverflow.com/questions/6445917/connect-failed-access-denied-for-user-rootlocalhost-using-password-yes):

mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' ;

mysql>flush privileges;

即赋全部权限。

经实验,问题未得到解决;

<2>直接清空root密码(eg:https://stackoverflow.com/questions/11223235/mysql-root-access-from-all-hosts):

mysql>update user set host='localhost' where user='root';

提示未选择数据表

mysql>use mysql;

再次执行,命令成功;

更新phpcode

{

$sqliconnect=new mysqli("localhost","test");

$query_result=$sqliconnect->prepare("select * from currentorder");

}

打印出的连接信息正常,出现新问题:$query_result打印出的信息为 BOOL(FALSE);

解决方法:在stackoverflow也发现此类问题(eg:https://stackoverflow.com/questions/8702241/mysqli-prepared-statement-returning-false)

根据don4of4网友回答,加入错误控制:

更新phpcode

{

if(!$query_result)

{

die("result erro:".$sqliconnect->error);

}

}

打印出的错误为 didn't select database;

于是参考官方手册(eg:http://php.net/manual/en/class.mysqli.php)

mysqli->select_db可以手动选择数据库;

更新phpcode{

$sqliconnect=new mysqli("localhost","test");

$sqliconnect->select_db("test");

}

调试信息出现了久违的object(mysqli_stmt)

至此,问题得到完整解决。

ps:我再进行mysqli的__construct()的时候已经传入了db_name,为何不起作用呢?
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐