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

发布于 2024-11-06 12:22:16 字数 836 浏览 0 评论 0原文

奇怪的问题 - 我有一个数据库类的实时副本(使用 PDO),工作正常。我的机器上有一个使用 WAMPServer 的副本,但没有。

连接字符串如下(来自类的片段):

$host = 'localhost';
$user = 'user';
$pass = 'password';
$dbname = 'my_dbname';

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

我收到的错误消息是:

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

我不知道为什么它不能在本地工作,而在线则可以。如果我将行本身更改为下面的内容,它就可以正常工作:

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

谢谢:)

Strange issue - I have a live copy of a database class (using PDO) that works fine. I have a copy on my machine using WAMPServer which does not.

The connection string is as follows (a snippet from the class):

$host = 'localhost';
$user = 'user';
$pass = 'password';
$dbname = 'my_dbname';

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

The error messages I get are:

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

I have no idea why it's not working locally whereas online its fine. If I change the line itself to the below it works fine:

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

Thanks :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

乖乖兔^ω^ 2024-11-13 12:22:16

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

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

Use double quotes new PDO("mysql:host=$host;dbname=$dbname", $user, $pass); otherwise the variables will not inserted into the string. Another way could be

new PDO(sprintf('mysql:host=%s;dbname=%s', $host, $dbname), $user, $pass);
眼眸印温柔 2024-11-13 12:22:16
self::$_instance = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);

您需要使用双引号。

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

You need to use double quotes.

泼猴你往哪里跑 2024-11-13 12:22:16

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

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

Variables are not parsed with single quotes. you have to enclose them with double quotes.

 self::$_instance = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
一抹微笑 2024-11-13 12:22:16

上面的两个答案对我来说都不起作用。我用:

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

Both answer above don't work by me. I use:

new PDO("mysql:host=".$host.";dbname=".$dbname, $user, $pass);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文