我正在编写一个数据库类,它将连接到我的pdo数据库。该类使用此配置文件来获取所需信息:
<?php
return [
'host' => '127.0.0.1',
'username' => 'root',
'password' => '',
'database_name' => 'books',
'database_type' => 'mysql',
'options' => []
];
这是数据库类:
<?php
class DB
{
public static function connect($config)
{
try {
return new PDO([
$config['database_type'] . ':host=' . $config['host'] . ';dbname=' . $config['database_name'],
$config['username'],
$config['password'],
$config['options']
]);
} catch(PDOException $e) {
die($e->getMessage());
}
}
}
我收到此错误:
致命错误:未捕获TypeError:PDO :: __ construct()需要参数 1是字符串,数组给出等等......
我想知道我做错了什么,sicne我在代码中没有看到任何语法错误。
答案 0 :(得分:1)
你有一组额外的括号:
return new PDO([
...
]);
......不应该分别有[和];他们将您的四个函数参数转换为单个数组参数。你只想
return new PDO(
...
);
HTH!