php 用户使用密码 NO 拒绝访问 MySQL 错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10873803/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MySQL Error Access denied for user using password NO
提问by halfer
I keep getting this error:
我不断收到此错误:
Warning: mysql_query() [function.mysql-query]: Access denied for user 'mcabinet'@'localhost' (using password: NO) in /home/mcabinet/public_html/games/db_edit/airportmadness4.php on line 3
Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in /home/mcabinet/public_html/games/db_edit/airportmadness4.php on line 3
No Database found.
Here is my entire .php page:
这是我的整个 .php 页面:
<?php
include("../templates/base/template2/mysql_connect.php");
$gametitle = "Airport Madness TEST";
$gamedescription = "DESCRIPtion testing a description. LOL !";
$image1url = "http://website-gamesite.com/games/images/lhfdsjk.jpg";
$categorycode = "adv";
$gametitle2 = "airportmadnesstest";
mysql_query("INSERT INTO Games VALUES (null,'$gametitle','$gamedescription','$image1url','$categorycode','0','$gametitle2'") or die ("Error Inserting Values into the Games Table");
?>
Thanks! I am just beginning with PHP and MySQL.
谢谢!我刚刚开始使用 PHP 和 MySQL。
EDIT: Here is the contents of the include file:
编辑:这是包含文件的内容:
<?php
$db_host = "localhost";
$db_username = "mcabinet_admin";
$db_password = "4jf8ido9A";
$db_name = "mcabinet_games";
@mysql_connect($db_host,$db_username,$db_password) or die ('MySQL Not found // Could Not Connect.');
@mysql_select_db("$db_name") or die ("No Database found.");
?>
After editing with your tips, I still receive the error.
使用您的提示进行编辑后,我仍然收到错误消息。
EDIT: I've made more changes. It is weird because when I get content from the database, the content is displayed fine, but with the errors still showing. And when I run the separate script, trying to add rows, it simply will not even connect.
编辑:我做了更多的改变。这很奇怪,因为当我从数据库中获取内容时,内容显示正常,但仍然显示错误。当我运行单独的脚本,尝试添加行时,它甚至无法连接。
采纳答案by halfer
RESOLVED!!! The Fix:
解决!!!修复:
Simple Change from:
简单更改自:
mysql_query("INSERT INTO Games VALUES (null,'$gametitle','$gamedescription','$image1url','$categorycode','0','$gametitle2'") or die ("Error Inserting Values into the Games Table");
to:
到:
mysql_query("INSERT INTO Games VALUES (null,'$gametitle','$gamedescription','$image1url','$categorycode','0','$gametitle2')") or die ("Error Inserting Values into the Games Table");
回答by Max Allan
Try to move the include above the first query row! Like this
尝试将包含移动到第一个查询行上方!像这样
<?php
include("../templates/base/template2/mysql_connect.php");
$query = mysql_query("SELECT * FROM Games WHERE id = '1' ");
To, make a MySQL-request you need to be connected first.
要发出一个 MySQL 请求,您需要先连接。
回答by Nadir Sampaoli
You should establish a connection beforerunning a query:
您应该在运行查询之前建立连接:
1) $link = mysqli_connect($host, $username, $password)
2) mysqli_select_db($link, $db_name);
3) $result = mysqli_query($link, $query);
回答by Xander Luciano
Try this:
尝试这个:
<?php
$db_host = "localhost";
$db_username = "mcabinet_admin";
$db_password = "4jf8ido9A";
$db_name = "mcabinet_games";
mysql_connect($db_host,$db_username,$db_password) or die ('MySQL Not found // Could Not Connect.');
mysql_select_db("$db_name") or die ("No Database found.");
$gametitle = "Airport Madness TEST";
$gamedescription = "DESCRIPtion testing a description. LOL !";
$image1url = "http://website-gamesite.com/games/images/lhfdsjk.jpg";
$categorycode = "adv";
$gametitle2 = "airportmadnesstest";
mysql_query("INSERT INTO Games VALUES (null,'$gametitle','$gamedescription','$image1url','$categorycode','0','$gametitle2'") or die ("Error Inserting Values into the Games Table");
?>
Also it appears that ur user is set to not use a password in phpmyadmin, would that be correct?
此外,您的用户似乎被设置为不使用 phpmyadmin 中的密码,这是正确的吗?
回答by halfer
Here's the problem:
这是问题所在:
mysqli_connect('$db_host','$db_username','$db_password')
or die ('MySQL Not found // Could Not Connect.');
This should be:
这应该是:
mysql_connect($db_host, $db_username, $db_password)
or die ('MySQL Not found // Could Not Connect.');
Since you've single-quoted your strings, you end up passing string literals of '$db_host'(etc) rather than the values you intended.
由于您对字符串进行了单引号,因此最终会传递'$db_host'(etc) 的字符串文字而不是您想要的值。
Also, it seems you are connecting with mysqlibut running queries using mysql. I'd guess that it would be worth using one or the other - they're two different libraries.
此外,您似乎正在连接mysqli但使用mysql. 我猜想使用其中一个是值得的——它们是两个不同的库。
回答by Hammad Khan
An interesting scenario is this
一个有趣的场景是这样的
<h1>Some heading</h1>
<?php
insert_data(a1,b1);
?>
<h2>other html stuff</h2>
<?php
include('database_connnection.php'); <--- problem is this line
function insert_data(a1,b1)
{
Query = "Insert INTO MYTABLE VALUES(a1,b1)";
}
?>
The above code will give error because insert_data() function is called ahead of invoking the include(database...) as it comes further down the code, hence you will get error. The fix is to move include(database) inside the function or ahead of invoking insert_data query
上面的代码会出错,因为 insert_data() 函数在调用 include(database...) 之前被调用,因为它在代码的后面,因此你会得到错误。解决方法是在函数内部或调用 insert_data 查询之前移动 include(database)
<?php
function insert_data(a1,b1)
{
include('database_connnection.php');
Query = "Insert INTO MYTABLE VALUES(a1,b1)";
}
?>

