我有一个由联合域名托管的网站。它和mysqli配合得很好,但我把它改成了pdo。现在我无法让数据库工作。我尝试将$host改为127.0.0.1和localhost。如果我使用localhost,我会得到错误 SQLSTATE[HY000] [2002] No such file or directory
.
如果我使用127.0.0.1 SQLSTATE[HY000] [2002] Connection refused
.
我也尝试过更改排序规则,但仍然不起作用。它在我的另一个域hostinger上运行得非常好。
$host = "localhost"; // I have tried using both 127.0.0.1 and localhost here
$db = "db_name"; //db name goes here
$username = "username"; //username goes here
$password = "password"; //password goes
try {
//Creating a PDO instance
$conn = new PDO("mysql:host=$host;dbname=$db", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$conn->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$conn->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
}catch(\PDOException $e){
// if connection fails, show PDO error
echo "Connection Failed: ". $e->getMessage();
}
暂无答案!
目前还没有任何答案,快来回答吧!