我創建了一個MySQL連接陣列,並把它傳遞給一個連接方法,例如:未定義的陣列偏移,但偏移存在
$database->connect(array(ROOT_DB_HOST,
ROOT_DB_NAME, ROOT_DB_USERNAME, ROOT_DB_PASSWORD));
當我print_r()
connect方法我進去的陣列我的期望:
Array
(
[0] => localhost
[1] => dbname
[2] => dbuser
[3] => dbpass
)
但是,在connect方法中,我將數組值傳遞給了DSN字符串,並且我收到了未定義的0,1,2,3偏移量。這裏是DSN字符串:
$this->dbh = new PDO('mysql:host='. $connection[0] .';dbname=' . $connection[1], $connection[2], $connection[3]);
所以我傳遞值作爲一個數組,並將其插入到PDO構造器和接收錯誤,我不知道是怎麼回事,什麼想法嗎?
方法:
public function connect($connection) {
try {
$this->dbh = new PDO('mysql:host='. $connection[0] .';dbname=' . $connection[1], $connection[2], $connection[3]);
$this->dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
} catch(PDOException $e) {
throw new Exception($e->getMessage());
}
}
錯誤:
Notice: Undefined offset: 0 in database.class.php on line 16 Notice: Undefined offset: 1 in database.class.php on line 16 Notice: Undefined offset: 2 in database.class.php on line 16 Notice: Undefined offset: 3 in database.class.php on line 16
Fatal error: Uncaught exception 'Exception' with message 'SQLSTATE[28000] [1045] Access denied for user 'apache'@'localhost' (using password: NO)' in database.class.php:20 Stack trace: #0 sandboxx.php(16): Database->connect(Array) #1 {main} thrown in database.class.php on line 200
發佈您得到的**確切**錯誤消息。 – Jocelyn 2013-05-11 22:10:04
你也可以發佈「connect()」方法的代碼嗎? – Niko 2013-05-11 22:10:57
當你使用var_dump數組和var_dump $連接時,你會得到什麼? – 2013-05-11 22:44:48