繁体   English   中英

WAMP 服务器连接错误:SQLSTATE [HY000] [1045] 用户“root”@“localhost”的访问被拒绝(使用密码:否)

[英]WAMP Server Connection error: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: NO)

请我需要以下代码的帮助; 我明白了

连接错误:SQLSTATE[HY000][1045] 用户 'root'@'localhost' 的访问被拒绝(使用密码:否)。

在这个错误之后,我在它下面得到另一个显示:

Fatal error: Uncaught Error: Call to a member function prepare() on null in C:\wamp64\www\user_login\objects\user.php on line 41

然后第三个显示:

Error: Call to a member function prepare() on null in C:\wamp64\www\user_login\objects\user.php on line 41

我对使用 class 还是很陌生; 请帮帮我。 谢谢

请在下面找到我的代码:

<?php
// used to get mysql database connection
class Database{

    // specify your own database credentials
    private $host = "localhost";
    private $db_name = "phplogin";
    private $username = "root";
    private $password = "";
    public $conn;

    // get the database connection

    public function getConnection(){

        $this->conn = null;
 
        try{
            $this->conn = new PDO("mysql:host=" . $this->host . ";dbname=" . $this->db_name, $this->username, $this->password);
        }catch(PDOException $exception){
            echo "Connection error: " . $exception->getMessage();
        }
 
        return $this->conn;
    }

}

?>

错误信息截图

基于错误的附加代码

<?php
// 'user' object

//require_once './config/database.php';

class User{
 
    // database connection and table name
    private $conn;
    private $table_name = "users";
 
    // object properties
    public $id;
    public $firstname;
    public $lastname;
    public $email;
    public $contact_number;
    public $address;
    public $password;
    public $access_level;
    public $access_code;
    public $status;
    public $created;
    public $modified;
 
    // constructor
    public function __construct($db){
        $this->conn = $db;
    }

    // check if given email exist in the database
    function emailExists(){
 
    // query to check if email exists
    $query = "SELECT id, firstname, lastname, password, access_level, status
            FROM " . $this->table_name . "
            WHERE email = ?
            LIMIT 0,1";
 
    // prepare the query
    $stmt = $this->conn->prepare($query);
 
    // sanitize
    $this->email=htmlspecialchars(strip_tags($this->email));
 
    // bind given email value
    $stmt->bindParam(1, $this->email);
 
    // execute the query
    $stmt->execute();
 
    // get number of rows
    $num = $stmt->rowCount();
 
    // if email exists, assign values to object properties for easy access and use for php sessions
    if($num>0){
 
        // get record details / values
        $row = $stmt->fetch(PDO::FETCH_ASSOC);
 
        // assign values to object properties
        $this->id = $row['id'];
        $this->firstname = $row['firstname'];
        $this->lastname = $row['lastname'];
        $this->access_level = $row['access_level'];
        $this->password = $row['password'];
        $this->status = $row['status'];
 
        // return true because email exists in the database
        return true;
    }
 
    // return false if email does not exist in the database
    return false;
   }

   // create new user record
   function create(){
 
    // to get time stamp for 'created' field
    $this->created=date('Y-m-d H:i:s');
 
    // insert query
    $query = "INSERT INTO
                " . $this->table_name . "
            SET
                firstname = :firstname,
                lastname = :lastname,
                email = :email,
                contact_number = :contact_number,
                address = :address,
                password = :password,
                access_level = :access_level,
                status = :status,
                created = :created";
 
    // prepare the query
    $stmt = $this->conn->prepare($query);
 
    // sanitize
    $this->firstname=htmlspecialchars(strip_tags($this->firstname));
    $this->lastname=htmlspecialchars(strip_tags($this->lastname));
    $this->email=htmlspecialchars(strip_tags($this->email));
    $this->contact_number=htmlspecialchars(strip_tags($this->contact_number));
    $this->address=htmlspecialchars(strip_tags($this->address));
    $this->password=htmlspecialchars(strip_tags($this->password));
    $this->access_level=htmlspecialchars(strip_tags($this->access_level));
    $this->status=htmlspecialchars(strip_tags($this->status));
 
    // bind the values
    $stmt->bindParam(':firstname', $this->firstname);
    $stmt->bindParam(':lastname', $this->lastname);
    $stmt->bindParam(':email', $this->email);
    $stmt->bindParam(':contact_number', $this->contact_number);
    $stmt->bindParam(':address', $this->address);
 
    // hash the password before saving to database
    $password_hash = password_hash($this->password, PASSWORD_BCRYPT);
    $stmt->bindParam(':password', $password_hash);
 
    $stmt->bindParam(':access_level', $this->access_level);
    $stmt->bindParam(':status', $this->status);
    $stmt->bindParam(':created', $this->created);
 
    // execute the query, also check if query was successful
    if($stmt->execute()){
        return true;
    }else{
        $this->showError($stmt);
        return false;
    }
 
}

public function showError($stmt){
     echo "<pre>";
         print_r($stmt->errorInfo());
     echo "</pre>";
}

}

?>

如果您在本地使用 MariaDb,您可以像这样设置用户:

CREATE USER 'databaseUser'@localhost IDENTIFIED BY 'databasePassword';
GRANT ALL PRIVILEGES ON databaseName.* TO 'databaseUser'@localhost;
FLUSH PRIVILEGES;

第一行创建用户和密码。

第二行将授予该用户对本地主机上每个数据库的访问权限。

第三行是必要的,以确保我们的用户对其数据库具有必要的权限。

然后在 PHP 脚本中使用如上设置的用户和密码。 我使用MySQL Workbench作为在本地处理我的数据库的工具; 在这里,我如上所述运行我的 SQL 脚本。

您可以查看所有用户

SELECT * FROM mysql.user;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM