php从文件中读取出的用户名不能用于连接数据库
程序员文章站
2022-05-01 20:36:26
...
$this->conn = mysql_connect($this->db_host, $this->db_user, $this->db_pwd) ;
if ( !($this->conn) )
{
die('Could not connect:'.mydsql_error('连接出错')) ;
}
echo '连接成功!
';
$this->db_host, $this->db_user, $this->db_pwd 都是从.ini文件中取值,取出来的值中主机地址和密码都可以用,但用户名却不能,如果是直接将用户名的字符串连接 mysql 就不会出错,如果是从文件中取出的用户名用于连接就会出现错误,如下:
Warning: mysql_connect(): Access denied for user 'noco '@'14.210.115.31' (using password: YES) in C:\xampp\htdocs\projects\ship_thing_swap\mysql\connection.php on line 52
Fatal error: Call to undefined function mydsql_error() in C:\xampp\htdocs\projects\ship_thing_swap\mysql\connection.php on line 55
请教各路大神这是神马情况???
if ( !($this->conn) )
{
die('Could not connect:'.mydsql_error('连接出错')) ;
}
echo '连接成功!
';
$this->db_host, $this->db_user, $this->db_pwd 都是从.ini文件中取值,取出来的值中主机地址和密码都可以用,但用户名却不能,如果是直接将用户名的字符串连接 mysql 就不会出错,如果是从文件中取出的用户名用于连接就会出现错误,如下:
Warning: mysql_connect(): Access denied for user 'noco '@'14.210.115.31' (using password: YES) in C:\xampp\htdocs\projects\ship_thing_swap\mysql\connection.php on line 52
Fatal error: Call to undefined function mydsql_error() in C:\xampp\htdocs\projects\ship_thing_swap\mysql\connection.php on line 55
请教各路大神这是神马情况???
回复讨论(解决方案)
$this->conn = mysql_connect($this->db_host, $this->db_user, $this->db_pwd) ;
if ( !($this->conn) )
{
die('Could not connect:'. mydsql_error('连接出错')) ;
}
拼写错误
观察到
'noco '@'14.210.115.31'
和
'noco'@'14.210.115.31'
的区别
显然是多了空格、回车、换行之类的字符
你是怎么读取 ini 文件的?
读取文件方式:
$this->db_host = fgets($openfile) ;
$this->db_user = fgets($openfile) ;
$this->db_pwd = fgets($openfile) ;
那是要去掉尾部的空白字符的(空格、回车、换行)
$this->db_host = trim(fgets($openfile)) ;$this->db_user = trim(fgets($openfile)) ;$this->db_pwd = trim(fgets($openfile)) ;
显然 'noco '@'14.210.115.31' 用户名后面多了个空格。