php SQLSTATE[HY000][2002] php_network_getaddresses: getaddrinfo failed: nodename 或 servname 提供,或未知
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34280673/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SQLSTATE[HY000][2002] php_network_getaddresses: getaddrinfo failed: nodename nor servname provided, or not known
提问by BuildNC
My problem:
我的问题:
I use MAMP and Git to view and edit my PHP files with SQL database connection on my mac and then push it to the web server. I recently added a file directory. Here is the file with the SQl database connection:
我使用 MAMP 和 Git 在我的 Mac 上通过 SQL 数据库连接查看和编辑我的 PHP 文件,然后将其推送到 Web 服务器。我最近添加了一个文件目录。这是带有 SQL 数据库连接的文件:
<?php
ob_start();
session_start();
//set timezone
date_default_timezone_set('America/New_York');
//database credentials
define('DBHOST','mysql.hostinger.co.uk');
define('DBUSER','u536535282_evan7');
define('DBPASS','...');
define('DBNAME','u536535282_dbsql');
//application address
define('DIR','http://w-o-l.ml/');
define('SITEEMAIL','[email protected]');
try {
//create PDO connection
$db = new PDO("mysql:host='.DBHOST.';port=8889;dbname='.DBNAME, DBUSER, DBPASS.'");
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
//show error
echo '<p>'.$e->getMessage().'</p>';
exit;
}
//include the user class, pass in the database connection
include('classes/user.php');
$user = new User($db);
?>
But yet it returns the following error on the page:
但它在页面上返回以下错误:
SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: nodename nor servname provided, or not known
SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: nodename 或 servname 提供,或未知
How do i fix it?
我如何解决它?
I cannot see my error so if someone could point it out, that would be helpful.
我看不到我的错误,所以如果有人能指出它,那会很有帮助。
采纳答案by miken32
Your quotes are all messed up.
你的报价全乱了。
$db = new PDO('mysql:host='.DBHOST.';port=8889;dbname='.DBNAME, DBUSER, DBPASS);
回答by Botle Khauhelo
I encountered the same Error with this line of code
我在这行代码中遇到了同样的错误
$dsn = 'mysql:dbname=$dbname;host=$host;port=$port';
and i was able to resolve the error by replacing the single quotes with double quotes as below.
我能够通过用双引号替换单引号来解决错误,如下所示。
$dsn = "mysql:dbname=$dbname;host=$host;port=$port";
i spent two days looking for solution online and couldn't find it until i decided to do the above changes, and it worked. I still don't understand why it doesn't work with single quotes.
我花了两天时间在网上寻找解决方案,直到我决定进行上述更改后才找到它,并且它奏效了。我仍然不明白为什么它不适用于单引号。