• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

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

php 搞代码 3年前 (2022-01-23) 27次浏览 已收录 0个评论
文章目录[隐藏]

$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 6本文来源gao@dai!ma.com搞$代^码!网7

搞gaodaima代码

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’ 用户名后面多了个空格。


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:php从文件中读取出的用户名不能用于连接数据库
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址