2017-02-20 117 views
0

夥計們,我有一個共同的問題。 我想使用PHP將數據從MySQL數據庫顯示到HTML頁面。mysql_connect():拒絕用戶'root'的訪問

使用此代碼:

<html> 
<head> 

<title>Pulse Sensor Data </title> 

</head> 
<body> 

<?php 

$servername = 'localhost'; 
$username = 'root'; 
$password = ''; 

// Connect to database server 
mysql_connect('192.168.1.106','root','','database') or die (mysql_error()); 

// Select database 
mysql_select_db('database') or die(mysql_error()); 

// SQL query 
$strSQL = "SELECT * FROM pulsesensor"; 

// Execute the query (the recordset $rs contains the result) 
$rs = mysql_query($strSQL); 

// Loop the recordset $rs 
while($row = mysql_fetch_array($rs)) { 

    // Write the value of the column id and value 
    echo $row['id'] . " " . $row['value'] . "<br />"; 

    } 

// Close the database connection 
mysql_close(); 
?> 

</body> 
</html> 

,但我得到

mysql_connect()函數:在C:拒絕訪問用戶 '根' @ 'XXX'(沒有使用密碼):\ XAMPP \ htdocs中\ html.php第16次 訪問被拒絕的用戶「根」 @「戴爾(使用密碼:否)

我改變了密碼相同的錯誤出現

mysql_connect()函數:訪問被拒絕的用戶 '根' @ 'XXX'(使用密碼:是)在C:\ XAMPP \ htdocs中\ html.php第16次 訪問被拒絕的用戶 '根' @」戴爾(使用密碼:YES)

我不知道該怎麼辦

+4

那麼你爲什麼要這樣分配'$服務器名稱= 'localhost' 的;'後使用'192.168.1.106'? –

+7

你需要停止使用'mysql_ *'功能。它們已被棄用多年,甚至不存在於當前的PHP版本中。請研究[PHP數據對象](http://php.net/manual/en/intro.pdo.php),通常簡稱爲PDO。 – sidyll

+0

可能的duplicateof http://stackoverflow.com/questions/8537531/access-denied-for-user-rootlocalhost-using-password-no –

回答

1

使用的IP地址和根連接字符串不是通過您正在使用的IP地址配置爲訪問主辦。您必須將其更改爲localhost或將該權限添加到您的mysql服務器以供root用戶使用。

我建議你不要那樣做,但是爲你的開發創建一個新的mysql用戶。

另外,從@sidyll您將不想使用mysql_*函數並使用PDO函數代替。

+0

非常感謝它的工作,但在localhost的環境,但如果我想遠程做到這一點,新用戶將幫助我在這種情況下?..對不起這些問題,我的第一個實驗@srayhunter –

+0

這是正確的。我建議創建一個用於該項目數據庫和表格的新用戶。然後,您可以限制新用戶對項目的數據庫和表的訪問權限。我會推薦研究mysql用戶以及訪問和權限的工作方式。 –

+0

謝謝! @srayhunter –

-1

試試這個:

mysql_connect($servername,'root','','database') or die (mysql_error()); 
+0

答案應該有解釋。 – chris85

相關問題