php db变量中的下划线导致问题 [英] underscore in php db variable causing problems

查看:91
本文介绍了php db变量中的下划线导致问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将数据库的所有登录信息存储在公共树之外的文件中的变量中,例如

I store all of my login information for databases in files outside of the public tree in variables such as

$hostname = '172.0.0.0';
$dbname = 'myname_mydbname';
$username = 'myname_user';
$pw = 'password';

这很标准.

问题是我正在使用的此特定主机要求将myname_附加到所有数据库和用户名的开头.当我存储这些字符串并将它们传递给PDO时,它会将用户名中的所有内容放在myname之后,并将所有密码字符串一起删除...如果我将用户名和密码作为字符串而不是变量放在函数中,则一切正常.我不知道该怎么做.有人可以帮忙吗?这就是代码中的功能.

The problem is that this particular hosting I am working with requires the myname_ to be appended to the front of all databases and user names. When I store these strings and pass them to a PDO it drops everything in the username after myname, and drops the password string all together... If I put the username and password in the function as strings instead of variables everything works. I am at my wits end. can anyone help? here is the function as it is in code.

不起作用:

$this -> DB = new PDO ("mysql:host={$hostname}; dbname={$dbname}", $username, $pw);

有效:

$this -> DB = new PDO ("mysql:host={$hostname};dbname={$dbname}", 'myname_user', 'password');

我希望这里的人能让我感到愚蠢……在此先感谢. -大卫

I am hoping someone here can make me feel stupid... thanks in advance. -David

该错误可能会帮助...

the error might help...

无法获取数据库句柄:SQLSTATE [28000] [1045]用户'myname'@'localhost'的访问被拒绝(使用密码:NO)

Failed to get DB handle: SQLSTATE[28000] [1045] Access denied for user 'myname'@'localhost' (using password: NO)

推荐答案

我在PDO文档中找不到任何内容,建议您可以在DSN字符串中指定用户名或密码-它是数据库源名称"而不是数据库源"名称和身份验证"字符串,实际上您没有使用密码,这应该是对此的提示,而用户名"myname"可能仅是由于大多数RDBM在未指定密码的情况下都使用$ USER环境变量进行连接(我必须假设是设置为我的名字")

Nothing I can find in the PDO documentation suggests that you can specify username or password in the DSN string - it is a "Database Source Name" not "Database Source Name and Authentication" String the fact you are using no password should be a hint to this, and the username being 'myname' is probably just because most RDBMs use the $USER environment var to connect if none is specified (which i must assume is set to 'myname')

即我认为您只需要使用额外的参数即可通过身份验证凭据

i.e. i think you simply have to use the extra parameters to pass the authentication credentials

这篇关于php db变量中的下划线导致问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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