2013-06-21 60 views
-1

我正在嘗試使用xampp php myAdmin連接mysql。mysql_connect():訪問拒絕用戶'root'@'localhost'(使用密碼:否)

用戶root默認情況下是在MySQL數據庫中。

這裏是我的代碼:

<html> 
<body> 
<?php 
    [email protected]_connect("localhost", "root", ''); 
    if (!$dbCon) 
    { 
     echo('<p> '.mysql_error().'</p>'); 
     exit(); 
    } 
    else 
    { 
     if([email protected]_select_db("crs",$dbCon)) 
    { 
     die("<p>".mysql_error()."</p>"); 
    } 
    } 

    $qry="Create Table Courses(cCode VarChar(5) Not NULL Primary Key, cTitle VarChar(50) Not NULL, CH INT)"; 

    if(@mysql_query($qry)) 
    { 
     echo('<p>Courses Table has been created</p>'); 
    } 
    else 
    { 
     echo('<p>Courses Table can not be created due to' .mysql_error(). '</p>'); 

    } 
    $qry="Insert INTO Courses Values('C001','Intro to computing',4)"; 
     if (@mysql_query($qry)) 
     { 
      echo('<p>C001 is inserted </p>'); 
     } 
     else 
     { 
      echo('<p>Courses Table cannot be created due to'.mysql_error().'</p>'); 

     } 
     $qry="Insert INTO Courses Values ('C002', 'Programming Fundamentals',4), ('C003', 'Object Oriented Programming',4), ('C004', 'Operating Systems',4), ('C005', 'Data Structures and Algorithms',4)"; 
     if(@mysql_query($qry)) 
     { 
     echo('<p>4 Rows have been inserted</p>'); 
     } 
     else 

      echo('<p>Multiple Rows cant be inserted due to'.mysql_error().'</p>')   



?> 
</body> 
</html> 
+0

您的代碼沒有任何問題。然而,mysql已經建立了root用戶需要密碼從本地主機連接,你沒有去你的電話mysql_connect。如果沒有以管理員用戶身份進行連接的方式,或者沒有人記住他們如何設置mysql,則無法真正幫助您。 –

回答

2

您不使用密碼和錯誤意味着root用戶沒有訪問或不能訪問,因爲它需要一個密碼。

+0

好吧,讓我再試一次 – AngelChoco

+0

是的,你是正確的工作現在..謝謝:) – AngelChoco

相關問題