php db 变量中的下划线导致问题
我将数据库的所有登录信息存储在公共树之外的文件中,例如
$hostname = '172.0.0.0';
$dbname = 'myname_mydbname';
$username = 'myname_user';
$pw = 'password';
“这是非常标准的”变量。
问题是我正在使用的这个特定主机需要将 myname_
附加到所有数据库和用户名的前面。当我存储这些字符串并将它们传递给 PDO 时,它会删除 myname 之后的用户名中的所有内容,并将密码字符串一起删除...如果我将用户名和密码作为字符串而不是变量放在函数中,则一切正常。我已经束手无策了。有人可以帮忙吗?这是代码中的函数。
不起作用:
$this -> DB = new PDO ("mysql:host={$hostname}; dbname={$dbname}", $username, $pw);
起作用:
$this -> DB = new PDO ("mysql:host={$hostname};dbname={$dbname}", 'myname_user', 'password');
我希望这里有人能让我感觉很愚蠢......提前致谢。 -大卫
这个错误可能会有所帮助......
无法获取数据库句柄:SQLSTATE[28000] [1045] 用户“myname”@“localhost”访问被拒绝(使用密码:NO)
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';
That's pretty standard.
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.
Does not work:
$this -> DB = new PDO ("mysql:host={$hostname}; dbname={$dbname}", $username, $pw);
works:
$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...
Failed to get DB handle: SQLSTATE[28000] [1045] Access denied for user 'myname'@'localhost' (using password: NO)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我在 PDO 文档中找不到任何内容表明您可以在 DSN 字符串中指定用户名或密码 - 它是“数据库源名称”而不是“数据库源名称和身份验证”字符串,您不使用密码的事实应该是一个提示这个,用户名是“myname”可能只是因为大多数 RDBM 使用 $USER 环境变量来连接,如果没有指定(我必须假设设置为“myname”),
即我认为你只需要使用额外的参数传递身份验证凭据
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
作为解决方法,我在存储信息的文件中构建了 get 函数,该函数返回字符串,并且它有效。超级奇怪,但它有效。
As a work around I built get functions in the file that the information is stored that return the strings, and it works. Super strange, but it works.
你应该通过附加引号得到相同的结果,
如果你想用引号存储它,pdo函数将去掉单引号(''),它会是你的sql语句中的'\'password\'',因为必须存储存储的引号带分隔符以防止自动转义。
你遇到了 {} 因为这将变量视为容器内的变量,所以它附加了 ' ' 当然 get 数组也会这样做(使用不同的字符,然后 php 在下一页加载时转义它们),因为你正在执行 java函数来使您的 get 函数,您本质上是附加未转义的引号。直到 PDO 函数逃脱它们。
you should get the same result from appending quotes
the pdo function will strip out the single quotes ( ' ' ) if you wanted to store it with quotes it would be '\'password\'' in your sql statement as stored quotes must be stored with delimiters to prevent auto escaping.
you came across the {} as this treats the variable as inside a container, so it appends ' ' of course the get array does this too (with different characters, then php escapes them on next page load ) since you're executing a java function to make your get functions, you are essentially appending quotes that are not escaped. Until PDO function escapes them.