常出現的Access denied for user 'root'@'localhost' (using password: YES)及其相關問題解決

/**
*author:田元
*
*company:whu eis
*contact:[email protected]
*
**/
從重裝數據庫開始:
軟件版本: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,爲何不起作用呢?
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章