Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/255.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP-PDO连接字符串中未替换的变量_Php_Pdo - Fatal编程技术网

PHP-PDO连接字符串中未替换的变量

PHP-PDO连接字符串中未替换的变量,php,pdo,Php,Pdo,奇怪的问题-我有一个数据库类的实时副本(使用PDO),运行良好。我的机器上有一个使用WAMPServer的副本,但它没有 连接字符串如下所示(类中的一个片段): 我收到的错误消息如下: Warning: PDO::__construct() [pdo.--construct]: php_network_getaddresses: getaddrinfo failed: No such host is known. in <path> on line 41 Warning: PDO:

奇怪的问题-我有一个数据库类的实时副本(使用PDO),运行良好。我的机器上有一个使用WAMPServer的副本,但它没有

连接字符串如下所示(类中的一个片段):

我收到的错误消息如下:

Warning: PDO::__construct() [pdo.--construct]: php_network_getaddresses: getaddrinfo failed: No such host is known. in <path> on line 41

Warning: PDO::__construct() [pdo.--construct]: [2002] php_network_getaddresses: getaddrinfo failed: No such host is kn (trying to connect via tcp://$host:3306) in <path> on line 41

谢谢:)

使用双引号
新建PDO(“mysql:host=$host;dbname=$dbname”,$user,$pass)否则变量将不会插入字符串中。另一种可能是

new PDO(sprintf('mysql:host=%s;dbname=%s', $host, $dbname), $user, $pass);

变量不使用单引号进行分析。你必须用双引号把它们括起来

 self::$_instance = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);

你需要使用双引号。

以上两个答案对我都不起作用。我使用:

 self::$_instance = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
new PDO("mysql:host=".$host.";dbname=".$dbname, $user, $pass);

那很有效-谢谢。但是为什么它在现场工作,而不是在本地使用单引号?可能在现场系统上定义了一个后备服务器和数据库。当您使用单引号时,它将如何替换变量mate?
self::$_instance = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
new PDO("mysql:host=".$host.";dbname=".$dbname, $user, $pass);