【问题标题】:WAMP Server Connection error: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: NO)WAMP 服务器连接错误:SQLSTATE [HY000] [1045] 用户“root”@“localhost”的访问被拒绝(使用密码:否)
【发布时间】:2022-01-26 10:35:59
【问题描述】:

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

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

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

致命错误:未捕获的错误:调用成员函数 prepare() on 第 41 行 C:\wamp64\www\user_login\objects\user.php 中的 null

然后第三个显示:

错误:在 null in 上调用成员函数 prepare() C:\wamp64\www\user_login\objects\user.php 在第 41 行

我还是个新手;请帮帮我。谢谢

请在下面找到我的代码:

<?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;
    }

}

?>

Error message screenshot

基于错误的附加代码

<?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>";
}

}

?>

【问题讨论】:

  • 您是否在 MySQL root 用户 ID 上设置了密码?
  • 在 MySQL 中创建一个用户总是更好,这与您正在构建的这个站点有关。给它一个密码,并设置它的权限以允许它只访问你用于这个站点的这个数据库。以后也可以更轻松地将其移动到实时站点
  • 检查您的 WAMP 问题是否正在运行。例如,如果您安装了 PHPMyAdmin,或者使用 MySQL Workbench,您可以使用它访问吗?
  • P.S. prepare() 的后续错误是由于发生连接故障时您没有告诉代码停止这一事实的结果。您已经捕获了异常,回显了错误,然后允许程序继续运行,这意味着它仍然会尝试在没有有效连接的情况下运行查询。它尝试使用null 变量作为连接对象。这不太合乎逻辑......
  • P.P.S.不要在任何输入数据上使用 htmlspecialchars 或 strip_tags。这是不必要的,充其量不会做任何有用的事情。更糟糕的是,它会以您意想不到的方式损坏或更改数据。您不需要以这种方式清理输入数据。只有在 输出 数据时才需要这些函数 - 即便如此,也只有在它们正在清理的事物可能存在潜在危险的情况下才需要这些函数。例如。将内容输出到网页时对 HTML 标记进行编码是有意义的,但如果您将相同的内容输出到 CSV 报告文件,则毫无意义。

标签: php mysql server wamp


【解决方案1】:

如果你在本地使用 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;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-10-08
    • 2020-01-16
    • 2020-08-20
    • 2018-07-22
    • 2017-11-05
    • 2015-08-16
    相关资源
    最近更新 更多