-3

我发现错误:在下一个脚本中找不到驱动程序。我现在正在为此烦恼一段时间,也许你看到了我的错。

这是我的代码:

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

欢迎任何帮助或建议?

4

1 回答 1

0

您对 PDO 构造函数的调用完全被不必要的引号和奇怪的参数顺序搞砸了。做就是了:

$dsn = sprintf('%s:host=%s;dbname=%s', $this->DatabaseType, $this->host, $this->database[$database]);
$connection = new PDO($dsn, $this->user1, $this->pass1);
于 2013-03-04T11:08:02.590 回答