Home  >  Article  >  Backend Development  >  php从文件中读取出的用户名不能用来连接数据库

php从文件中读取出的用户名不能用来连接数据库

WBOY
WBOYOriginal
2016-06-13 12:14:24782browse

php从文件中读取出的用户名不能用于连接数据库
$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
请教各路大神这是神马情况???
------解决思路----------------------
$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 = trim(fgets($openfile)) ;<br />$this->db_user = trim(fgets($openfile)) ;<br />$this->db_pwd = trim(fgets($openfile)) ;

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn