我有一个类可以毫无问题地连接到数据库。在扩展该类时,相同的连接失败。凭证在 phpMyAdmin 和命令行中有效。
尝试连接到服务器时出现的错误:
1045: Access denied for user 'phpClasses'@'Jesse-Server' (using password: NO)
我也尝试了具有相同结果的替代凭据。
下面是父类、子类、调用者模型的构造函数和属性定义:
//Database.php
private $dbhost;
private $dbname;
private $dbusername;
private $dbuserpassword;
private $dbtableprefix;
private $dbport;
protected $dbC;
public function __construct($ndbhost = "localhost", $ndbname = "", $ndbusername = "", $ndbuserpassword = "", $ndbtableprefix = " ", $ndbport = 3600)
{
$this->dbhost = $ndbhost; // Default = localhost
$this->dbname = $ndbname; // Default = None
$this->dbusername = $ndbusername; //Default = None
$this->dbuserpassword = $ndbuserpassword; // Default = None
$this->dbtableprefix = $ndbtableprefix; //Default = None sets the prefix before the table name so that it can be used with the same db with different tables.
$this->dbport = $ndbport; // Default = 3600
// starting the connection
@ $this->dbC = new mysqli($this->dbhost, $this->dbusername, $this->dbuserpassword, $this->dbname);//, $this->dbport);
if ($this->dbC->connect_error) {
//catch error connecting.
die("There was a connection error while attempting to connect to the database " . $this->dbname . " on " . $this->dbhost . ":" . $this->dbport . ". The following is the error that we received back: <strong>" . $this->dbC->connect_errno . ": " . $this->dbC->connect_error . "</strong>\n Please correct this issue, if you need assistance see your database or IT administrator.");
}else{
//echo "Connected to " . $this->dbname . " on " . $this->dbhost . ":" . $this->dbport;
}
}
现在的 child
//QueryBuilder.php
protected $dbC;
private $host;
private $dbName;
private $dbUser;
private $dbPassword;
private $tablePrefix;
private $dbport;
function __construct($ndbhost = "localhost", $ndbname = "", $ndbusername = "", $ndbuserpassword = "", $ndbtableprefix = " ", $ndbport = 3600){
//default Constructor
$this->host = $ndbhost;
$this->dbName = $ndbname;
$this->dbUser = $ndbusername;
$this->dbPassword = $ndbuserpassword;
$this->tablePrefix = $ndbtableprefix;
$this->dbport = $ndbport;
echo $this->host . ',';
echo $this->dbName . ',';
echo $this->dbUser . ',';
echo $this->dbPassword . ',';
echo $this->tablePrefix . ',';
echo $this->dbport;
parent::__construct($this->host, $this->dbName, $this->dbUser, $this->dbpassword, $this->tableprefix, $this->dbport);
$this->dbC = parent::getdbconnection();
}
模型
//model.php
require("./Class/Database.php");
require("./Class/QueryBuilder.php");
$result=false;
$host = '192.168.1.2'; //or localhost , same thing, also tried 127.0.0.1
$database = 'phpclasses';
$dbuser = 'phpClasses';
$dbuserpw = 'test1234';
//now I want to see that the connection has been made.
//$db = new DatabaseClass($host, $database, $dbuser, $dbuserpw);
$qb = new QueryBuilderClass($host, $database, $dbuser, $dbuserpw);
感谢大家的宝贵时间和帮助;
杰西芬德
最佳答案
在 child 身上。 $this->dbpassword 与 $this->dbPassword 有大小写混合拼写错误。 $tablePrefix 与 $tableprefix 相同
关于php - 错误 1045 : access denied for user 'root' @'localhost' using password NO. .. 通过扩展类连接时,其他,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43221938/