mysql_connect:php_network_getaddresses:getaddrinfo失败:使用文件值未知此类主机 [英] mysql_connect: php_network_getaddresses: getaddrinfo failed: No such host is known using file values

查看:57
本文介绍了mysql_connect:php_network_getaddresses:getaddrinfo失败:使用文件值未知此类主机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如标题上所写,我遇到了这个问题. 我有允许我连接到数据库的数据存储在一个文件中.获取这些信息后,我尝试与数据库建立通信,但收到警告

As wrote on the title I have this issue. I have the data that allows me to connect to my database stored in a file. After getting those infos I tried to establish a communication with the database but I receive the warning

警告:mysql_connect():php_network_getaddresses:getaddrinfo失败:未知此类主机.在第21行的C:\ wamp \ www \ jstool \ connect.php中

Warning: mysql_connect(): php_network_getaddresses: getaddrinfo failed: No such host is known. in C:\wamp\www\jstool\connect.php on line 21

两次,即使值正确,连接的结果也不是什么(也就是我尝试通过连接记录下来的值). 这是txt文件:

twice and the result of the connect is nothing even if the values are correct (a.k.a. if I tried the connect writing down the values it works). Here's the txt file:

localhost
root
pwd

代码如下:

ini_set("auto_detect_line_endings", true);

$handle = fopen("config.txt", "r");
if ($handle) {
   $i=0;
   while (($line = fgets($handle)) !== false) {
        $ln[$i]= $line;
        $i++;
   }
}

define("SERVER", $ln[0]);
define("USER", $ln[1]);
define("PASSWORD", $ln[2]);
$db = mysql_connect(SERVER,USER,PASSWORD);

if (!$db) {
    [...]
}
else {
    [.. reach database ..]
}

如果我只是写

$db = mysql_connect("localhost", "root", "pwd");

您可以看到它是txt文件中包含的值,它工作正常.

which as you can see are the values contained in the txt file, it works fine.

推荐答案

尝试使用 trim()

define("SERVER", trim($ln[0]));
define("USER", trim($ln[1]));
define("PASSWORD", trim($ln[2]));

来自 fgets()手册

当读取长度-1个字节或换行符(其中 包含在返回值中)

Reading ends when length - 1 bytes have been read, or a newline (which is included in the return value)

所以您的阅读内容包括换行符.

so your read includes the newlines.

这篇关于mysql_connect:php_network_getaddresses:getaddrinfo失败:使用文件值未知此类主机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆