SQLSTATE [HY000] [1045]拒绝用户访问

时间:2014-01-06 14:06:26

标签: php mysql

任何人都知道这意味着什么?不是一个非常有经验的程序员。

SQLSTATE [HY000] [1045]拒绝访问用户“@” *。' (使用密码:否)SELECT * FROM ** _Students

StudentDataSet中的

代码     

require_once ('Models/StudentData.php'); 
require_once ('Models/Database.php');


class StudentsDataSet {
protected $_dbHandle, $_dbInstance;

public function __construct() 

{
    $this-> _dbInstance = Database::getInstance();
    $this-> _dbHandle = $this-> _dbInstance-> getdbConnection();
}

public function fetchAllStudents() {
$sqlQuery = 'SELECT * FROM MUD193_Students'; // put your students table name
echo $sqlQuery; //helpful for debugging to see what SQL query has been created

$statement = $this->_dbHandle->prepare($sqlQuery); // prepare PDO statement
$statement->execute(); // execute the PDO statement

$dataSet = [];
while ($row = $statement->fetch()) {
$dataSet[] = new StudentData($row); }
return $dataSet; }

StudentData中的代码

<?php

class StudentDataClass {

private $_id, $_firstName, $_lastName, $_international, $_courseID;

public function __construct($dbRow) {
    $this->_id = $dbRow['id'];
    $this->_firstName = $dbRow['first_name'];
    $this->_lastName = $dbRow['last_name'];
    if ($dbRow['international'])
        $this->_international = 'yes';
    else
        $this->_international = 'no'; $this->_courseID = $dbRow['courseID'];
}

public function getStudentID() {
    return $this->_id;
}

}

Databse中的代码

<?php

class Database {

protected static $_dbInstance = null;
protected $_dbHandle;

public static function getInstance() {
    $username = '****';
    $password = '****';
    $host = '*******';
    $dbName = '****';
    if (self::$_dbInstance === null) { 
        self::$_dbInstance = new self($username, $password, $host, $dbName);
    }
    return self::$_dbInstance;
    }

    private function __construct($username, $password, $host, $database) {
    try {
        $this->_dbHandle = new PDO("mysql:host=$host; dbname=
    $database;  $username, $password");
    } // creates database handle with connection info
    catch (PDOException $e) { // catch any failure to connect to database
        echo $e->getMessage();
    }
    }

    public function getdbConnection() {
    return $this->_dbHandle; // returns the database handle to be used elsewhere
    }

    public function __destruct() {
    $this->_dbHandle = null; // destroys the destroys the database handle
    }

    }

2 个答案:

答案 0 :(得分:0)

这是Access denied error。您应该检查您登录的数据库用户名和密码

答案 1 :(得分:0)

定义你的;

  • 数据库名称
  • 数据库用户名
  • 数据库用户密码

可能脚本无法访问您定义的变量。

如果您提供更多信息,例如您用于连接数据库的语言,我们可以为您提供更好的帮助。