2011-02-24 87 views
1

我已經創建了一個html表單(demo.html)& POST它對php文件(demo1.php)的值。然後,我在MySQL &創建的數據庫在PHP文件(demo1.php)寫的連接代碼,但它提供了以下錯誤PHP MySQL驗證錯誤

警告:mysql_connect()函數[function.mysql-連接]:拒絕訪問用戶「根'@' 本地主機」(使用密碼:是)在C:\ XAMPP \ htdocs中\ demo1.php第8行 錯誤連接到MySQL

<?php 
// contact to database 
$dbhost = 'localhost'; 
$dbuser = 'root'; 
$dbpass = 'password'; 

$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql'); 
//$conn = mysql_connect("localhost","root","password") or die ('Error connecting to mysql'); 

$dbname = 'test'; 
mysql_select_db($dbname,$conn); 

//$connect = mysql_connect("localhost", "root", "pass") or die ("Error , check your server connection."); 

//mysql_select_db("test"); 



//Get data in local variable 

$v_name=$_POST['name']; 

$v_email=$_POST['email']; 

$v_msg=$_POST['msg']; 



// check for null values 

if ($v_name=="" or $v_msg=="") 

echo "All fields must be entered, hit back button and re-enter information"; 

else{ 

$query="insert into contact(name,email,msg) values('$v_name','$v_email','$v_msg')"; 

mysql_query($query) or die(mysql_error()); 

echo "Your message has been received"; 

mysql_close($conn); 

} 

?> 
+0

MySQL服務器是否正在運行?你確定用戶名和密碼正確嗎? – TuteC 2011-02-24 05:51:31

回答

2

密碼用戶root不正確。本地(家庭)服務器上

默認密碼是空的,所以我認爲這可以幫助:

$dbpass = ''; 
0
For Local Xampp username-root password --'' 

<?php 
// contact to database 
$dbhost = 'localhost'; 
$dbuser = 'root'; 
$dbpass = ''; 

$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql'); 
?> 
0

造成這種情況的主要原因是密碼錯誤。設置xampp時是否指定了根密碼?如果沒有,那麼將密碼保留空白,因爲上述隱含狀態爲