在php中执行mysql_query时出现访问被拒绝错误 [英] Getting access denied error when executing mysql_query in php

查看:42
本文介绍了在php中执行mysql_query时出现访问被拒绝错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

警告我的 php 文件..

Warning for my php file..

警告:mysql_query() [function.mysql-query]:C:\Program Files\xampp\htdocs\shadaab\register.php 中的用户 'ODBC'@'localhost' 访问被拒绝(使用密码:NO)第 25 行

Warning: mysql_query() [function.mysql-query]: Access denied for user 'ODBC'@'localhost' (using password: NO) in C:\Program Files\xampp\htdocs\shadaab\register.php on line 25

警告:mysql_query() [function.mysql-query]:无法在 C:\Program Files\xampp\htdocs\shadaab\register.php 中第 25 行建立到服务器的链接用户 'ODBC'@'localhost' 访问被拒绝(使用密码:NO)

Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in C:\Program Files\xampp\htdocs\shadaab\register.php on line 25 Access denied for user 'ODBC'@'localhost' (using password: NO)

我的连接文件:

$localhost='localhost';
$username='root';
$password='';
$dbname='school';

$con=mysql_connect('$localhost','$username','$password');
mysql_select_db('$dbname',$con);

推荐答案

最终更新:

我与 Aditii 建立了一个聊天会话,我们在那里进行了一些调试.

Final Update:

I opened a chat session with Aditii and we did some debugging there.

事实证明,问题在于她已从启用了短标签的服务器升级到没有启用短标签的服务器.结果,她包含在短标签中的所有代码都没有被执行,这包括她的配置文件.

As it turns out, the problem was that she had upgraded from a server which had short tags enabled to one that did not. As a result, all of her code that was enclosed in short tags was not being executed, and this included her configuration file.

问题中的错误是由于 mysql_connect() 函数没有被执行,因为整个文件没有被执行.依赖于该连接的 mysql_query() 调用因此失败.

The error in the question was due to the mysql_connect() function not being executed because that whole file was not being executed. A mysql_query() call that relied on that connection failed because of this.

在更正她的短标签以使用完整的 <?php 标签后,该文件被执行.正如我在对这个问题的原始回答中所讨论的那样,传递给 mysql_connect() 的变量周围的单引号确实最终导致了问题.建议去掉那些单引号后,连接成功,问题解决.

After correcting her short tags to use the full <?php tag, that file executed. As discussed in my original answer to this question, the single quotes around the variables being passed to mysql_connect() did end up causing problems. After advising that those single quotes be removed, the connection succeeded and the problem was resolved.

对于任何感兴趣的各方,可以在此处找到整个调试会话.

The entire debugging session, for any interested parties, can be found here.

第一次更新:

仔细查看后,有两部分不符合我的原始回复(我将其保留在此答案的底部,因为其中仍有一些不错的信息).

After looking at this a bit closer, there are two pieces that don't fit my original response (which I've kept at the bottom of this answer, since it still has some good info in it).

首先,错误是由 mysql_query() 而不是 mysql_connect() 触发的.这可能是因为没有围绕连接进行错误处理,因此直到稍后才会注意到错误.尝试在连接周围添加一些错误处理:

First, the error is being triggered by mysql_query() not mysql_connect(). This is probably because there is no error handling surrounding the connection, so the error doesn't get noticed until later. Try adding some error handling around the connection:

if (!($con = mysql_connect('$localhost','$username','$password'))) {
  echo mysql_error();
}

if (!mysql_select_db('$dbname',$con)) {
  echo mysql_error();
}

在上面,一旦出现错误就会显示错误.

In the above, errors will be displayed as soon as the error occurs.

我看到的第二件事是您正在使用 ODBC 用户连接到 localhost.如果错误是由上面显示的连接代码触发的,则 没有任何意义.如果您的问题是我在下面解释的单引号问题,您将使用名为 $username 的用户登录,如果不是,则使用 root 登录.

The second thing I see is that you're connecting to localhost with the ODBC user. This makes no sense if the error is being triggered by the connection code you've shown above. Either you would be logging on with a user called $username if your problem is the single quote issue I explain below, or root if you're not.

看起来 PHP 正在尝试连接您的 PHP 设置的默认凭据(在您的 php.ini 文件中找到),而不是您提供的凭据.这使我相信您向我们展示的行实际上并不是正在执行的行.您确定您没有尝试连接代码中的其他地方吗?如果是,我怀疑您的错误在于代码的那部分.

It looks like PHP is trying to connect with the default credentials for your PHP setup (found in your php.ini file), rather than the credentials you've supplied. This leads me to believe that the lines you're showing us aren't actually the lines being executed. Are you sure that you're not trying to connect elsewhere in your code? If you are, I suspect that your error lies in that portion of the code.

也有可能(我必须检查)当您尝试针对空连接运行 mysql_query() 时,也许 PHP 正在尝试为您创建连接.不过,我不会指望这一点.

It's also possible (I would have to check) that when you're attempting to run mysql_query() against a null connection, maybe PHP is trying to create a connection for you. I wouldn't count on that though.

我建议您尝试运行我上面提供的更新代码,它使用 mysql_error() 函数来显示发生的错误.您甚至可以在这些块中添加一些您自己的 echo 语句,用于一些老式的 printf 调试.

I suggest you try running the updated code I've provided above, which uses the mysql_error() function to show errors as they happen. You could even add in some echo statements of your own in those blocks, for some good old fashioned printf debugging.

原答案:

问题在于您将变量括在单引号内.如果将它们用单引号括起来,PHP 不会将变量计算为值.例如,不是将 root 作为第二个参数传递,而是传递 $username.最后,您尝试使用一个名为 $username 且密码为 $password 的用户登录名为 $localhost 的服务器.

The problem is that you're wrapping your variables inside single quotes. If you wrap them in single quotes, PHP doesn't evaluate the variable to the value. For example, rather than passing root as the second argument, you're passing $username. In the end, you're trying to log into a server called $localhost with a user called $username with a password of $password.

查看字符串的 PHP 文档页面.它解释了这一点:

Take a look at the PHP documentation page for Strings. It explains this:

注意:与双引号和heredoc语法不同,特殊字符的变量和转义序列在出现在单引号字符串.

Note: Unlike the double-quoted and heredoc syntaxes, variables and escape sequences for special characters will not be expanded when they occur in single quoted strings.

如果你想在字符串中使用变量,你需要使用引号,而不是单引号.然而,在这种情况下,您甚至根本不需要将这些变量用引号括起来,因为它们的值已经是字符串.您可以单独传递变量.试试这个:

If you want to use variables inside of a string, you need to use double quotes, not single. In this case, however, you don't even need to wrap these variables in quotes at all, because their values are already strings. You can just pass the variable all by itself. Try this:

$con=mysql_connect($localhost, $username, $password);
mysql_select_db($dbname, $con);

这篇关于在php中执行mysql_query时出现访问被拒绝错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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