2013-03-04 99 views
-3

我發現錯誤:在下一個腳本中找不到驅動程序。現在我已經頭痛了一段時間了,也許你會看到我的錯。找不到驅動程序(php,PDO)

這是我的代碼:

class ConnectStation { 
    private $host = "localhost"; 
    private $DatabaseType = "mysql"; 
    private $database = array (
    1 => "Database_one", 
    2 => "Database_two", 
    3 => "Database_true" 
); 
private $user1 = "MyUsername1"; 
private $pass1 = "MyPassword1"; 
private $user2 = "MyUsername2"; 
private $pass2 = "MyPassword2"; 

public function ConnectDB($user, $database){ 
if ($database==""){$database=1;} 
try{ 
    switch ($user){ 
     case "ReadOnly": 
     $connection = new PDO("'".$this->DatabaseType.":host=".$this->host.";dbname=".$this->database[$database]."', '".$this->user1."', '".$this->pass1."'"); 
     $connection->exec('SET CHARACTER SET utf8'); 
     return $connection; 
     break; 
     case "Admin": 
     $connection = new PDO("'".$this->DatabaseType.":host=".$this->host.";dbname=".$this->database[$database]."', '".$this->user2."', '".$this->pass2."'"); 
     $connection->exec('SET CHARACTER SET utf8'); 
     return $connection; 
     break; 
     } 
    } 
catch(PDOException $e){ 
    echo $e->getMessage(); 
    } 
} 
} 

奧凱迄今比我使用的查詢發送到數據庫的紙條連接。代碼是這樣的:

$userCard = new ConnectStation; 

$query = "SELECT username FROM users"; 
foreach ($this->ConnectDB('ReadOnly', 1)->query($query) as $row){ 
echo $row['username']."<br>"; 
} 

任何幫助礦石的建議是值得歡迎的?

+2

您確定它是POD而不是PDO嗎? – Achrome 2013-03-04 10:55:05

+0

是否有一個名爲「mysql」的驅動程序? **請注意主引號** – deceze 2013-03-04 10:56:43

+0

正如Aswin所說...其PDO不是POD – coolguy 2013-03-04 10:56:44

回答

0

您對PDO構造函數的調用完全被不必要的引號和奇怪的參數順序搞砸了。只要這樣做:

$dsn = sprintf('%s:host=%s;dbname=%s', $this->DatabaseType, $this->host, $this->database[$database]); 
$connection = new PDO($dsn, $this->user1, $this->pass1); 
+0

謝謝昨天回答!它工作完美! – 2013-03-05 13:49:22

相關問題