2017-04-05 68 views
1

我有一個連接到數據庫沒有問題的類。擴展該類時,同一連接失敗。憑據在phpMyAdmin和命令行中工作。錯誤1045:訪問拒絕用戶'root'@'localhost'使用密碼NO ...當通過擴展類連接時,其他人

錯誤嘗試連接到服務器時,我得到:

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

現在孩子

//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); 

我感謝大家的時間和你的幫助;

傑西芬德

+0

'getdbconnection()'的代碼在哪裏?你在php日誌中看到錯誤1045嗎? – mkaatman

+0

'192.168.1.2,phpclasses,phpClasses,test1234,,3600' 是從QueryBuilderClass打印存儲在子類屬性中的值 –

+0

@mkaatman ::所做的一切都是通過ref返回到子類的連接來被使用...但在這裏它是::'保護函數getdbconnection()返回$ this-> dbC; }' 我不確定它是否會顯示在php日誌中,我想我可以找到它。讓我看起來和生病回到你身邊 –

回答

2

在孩子。對於$ this-> dbpassword vs $ this-> dbPassword,您有混合大小寫拼寫錯誤。與$ tablePrefix和$ tableprefix相同

+0

這種情況下,有一個近距離投票選項 – Phil

+0

是的,我在本地重新創建了他的整個示例以仔細檢查發生了什麼。自從我遇到麻煩以後,我覺得有必要給他一個答案。如果用戶只是簡單地使用它,php錯誤日誌就會使問題非常明顯。無論如何,我爲錯字增加了一次近距離投票。 – mkaatman

+0

對不起 - 我被難住了,我的錯誤日誌沒有這個位置。 –

相關問題