当前位置: 代码迷 >> PHP >> php从文件中读取出的用户名不能用来连接数据库
  详细解决方案

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

热度:12   发布时间:2016-04-28 17:54:16.0
php从文件中读取出的用户名不能用于连接数据库
$this->conn = mysql_connect($this->db_host, $this->db_user, $this->db_pwd) ;
if ( !($this->conn) )
{
die('Could not connect:'.mydsql_error('连接出错')) ;
}
echo '连接成功!<br>';
$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)) ;
$this->db_user = trim(fgets($openfile)) ;
$this->db_pwd = trim(fgets($openfile)) ;
  相关解决方案