問題描述
今天為了遷移到php 5.30,我把最新版的lampp卸載重裝了,突然一個很簡單的app連接mysql數據庫失敗.我正在使用 PDO 進行連接,并收到以下錯誤:
Today, I removed and reinstalled the latest version of lampp in order to move to php 5.30, and suddenly a very simple app is failing to connect to the mysql database. I'm using PDO to connect, and receiving the following error:
Warning: PDO::__construct() [pdo.--construct]: [2002] Invalid argument (trying to connect
via unix://) in /home/raistlin/www/todoapp/home.php on line 9
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [2002]
Invalid argument' in /home/raistlin/www/todoapp/home.php:9 Stack trace: #0
/home/raistlin/www/todoapp/home.php(9): PDO->__construct('mysql:host=loca...', 'USER',
'PASSWORD') #1 {main} thrown in /home/raistlin/www/todoapp/home.php on line 9
為了調試,我目前沒有捕捉到錯誤.
I am not catching the error at the moment, for the sake of debugging it.
以下代碼足以在我的系統上復制該問題:
The following code is enough to replicate the issue on my system:
<?php
$DBACCESS = array(
"connstring"=>"mysql:host=localhost;dbname=todoapp",
"host"=>"localhost",
"user"=>"user",
"password"=>"password",
"todoapp"=>"todoapp"
);
echo implode('<br >',$DBACCESS);
$dbh = new PDO($DBACCESS['connstring'],$DBACCESS['user'],$DBACCESS['password']);
$dbh = null;
?>
在網上查找,我發現一兩個其他人遇到了同樣的問題,但他們都沒有收到回復,更不用說有效的回復了.有誰知道發生了什么?我在配置中遺漏了什么嗎?我需要做什么來修復它?
Looking online, I've found one or two other people with the same issue, but none of them have received a response, much less a working one. Does anyone know what is happening? Is there something I missed in the configuration? What do I need to do to fix it?
推薦答案
通常意味著你需要指定 TCP/IP (1),或者告訴 MySQL 你的 Unix socket 在哪里 (2):
Usually means that you need to specify TCP/IP (1), or tell MySQL where your Unix socket is (2):
- "mysql:host=127.0.0.1" 或 "mysql:host=localhost;port=3306"
- "mysql:unix_socket=/var/run/mysqld/mysqld.sock"
這篇關于在 PHP 中創建到 PDO 的連接時出錯的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!