Php 使用密码NO的用户的MySQL错误访问被拒绝
我不断地发现这个错误:Php 使用密码NO的用户的MySQL错误访问被拒绝,php,mysql,mysql-error-1045,Php,Mysql,Mysql Error 1045,我不断地发现这个错误: 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
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.
这是我的整个.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");
?>
在使用您的提示进行编辑后,我仍然收到错误
编辑:我做了更多的更改。这很奇怪,因为当我从数据库中获取内容时,内容显示得很好,但仍然显示错误。当我运行单独的脚本,试图添加行时,它甚至无法连接。尝试将include移到第一个查询行上方! 像这样
<?php
include("../templates/base/template2/mysql_connect.php");
$query = mysql_query("SELECT * FROM Games WHERE id = '1' ");
您应该在运行查询之前建立连接:
1) $link = mysqli_connect($host, $username, $password)
2) mysqli_select_db($link, $db_name);
3) $result = mysqli_query($link, $query);
试试这个:
<?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");
?>
问题出在这里:
mysqli_connect('$db_host','$db_username','$db_password')
or die ('MySQL Not found // Could Not Connect.');
这应该是:
mysql_connect($db_host, $db_username, $db_password)
or die ('MySQL Not found // Could Not Connect.');
由于只引用了字符串,因此最终传递的是“$db_host”(etc)的字符串文本,而不是预期的值
此外,您似乎正在连接mysqli
,但正在使用mysql
运行查询。我想它值得使用其中一个——它们是两个不同的库。解决了!!!修复方法:
简单更改为:
mysql_query("INSERT INTO Games VALUES (null,'$gametitle','$gamedescription','$image1url','$categorycode','0','$gametitle2'") or die ("Error Inserting Values into the Games Table");
致:
一个有趣的场景是这样的
<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)";
}
?>
某个标题
其他html内容
上面的代码将给出错误,因为在调用include(数据库…)之前会调用insert_data()函数,因为它会在代码的后面进一步调用,因此您将得到错误。修复方法是将include(数据库)移动到函数内部或在调用insert_数据查询之前
<?php
function insert_data(a1,b1)
{
include('database_connnection.php');
Query = "Insert INTO MYTABLE VALUES(a1,b1)";
}
?>
您忽略了连接到数据库的实际代码。由于您的问题正是与连接到数据库有关,因此最有可能的原因是连接到数据库的位置。请在您的MySql\u connect.php
文件中删除MySql调用前面的@
符号-这可能会掩盖错误。谢谢,但这仍然不起作用。还有什么想法吗?我已经将上面的代码编辑到了最新版本。如果您将mysqli\u connect()
编辑到mysql\u connect()
应该可以工作了!mysql\u connect
调用中的单引号将导致一个问题-当前该主机名为$db\u host
,而不是localhost
(其他连接值依此类推)。删除引号以使其正常工作:)
Haha-oops,从他的代码中带出的引号:P现在修复了它,但实际上,我的用户确实有密码。这就是为什么我感到困惑。是否将内容添加到数据库本身有问题?下面是我的数据库的外观。ID是自动递增的,每次用户访问我网站上的某个页面时,视图计数都是+。我想我的帖子会有所帮助,因为你已经将我的更改编辑到了你的问题中!他失踪了
<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)";
}
?>
<?php
function insert_data(a1,b1)
{
include('database_connnection.php');
Query = "Insert INTO MYTABLE VALUES(a1,b1)";
}
?>