使用密码NO的用户拒绝MySQL错误访问

时间:2012-06-03 20:43:46

标签: 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 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");

?>

谢谢!我刚开始使用PHP和MySQL。

编辑:以下是包含文件的内容:

<?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.");


?>

使用您的提示进行编辑后,我仍然收到错误。

编辑:我做了更多改动。这很奇怪,因为当我从数据库中获取内容时,内容显示正常,但仍然显示错误。当我运行单独的脚本时,尝试添加行,它甚至都不会连接。

6 个答案:

答案 0 :(得分:4)

尝试将include移到第一个查询行上方! 喜欢这个

<?php
  include("../templates/base/template2/mysql_connect.php");
  $query = mysql_query("SELECT * FROM Games WHERE id = '1' "); 

要发出MySQL请求,您需要先连接。

答案 1 :(得分:2)

试试这个:

<?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");

?>

此外,您的用户似乎设置为不在phpmyadmin中使用密码,这是否正确?

答案 2 :(得分:2)

问题在于:

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'(等)的字符串文字,而不是您想要的值。

此外,您似乎正在使用mysqli进行连接,但使用mysql运行查询。我猜它值得使用其中一个 - 它们是两个不同的库。

答案 3 :(得分:1)

您应该在运行查询之前建立连接

1) $link = mysqli_connect($host, $username, $password)
2) mysqli_select_db($link, $db_name);
3) $result = mysqli_query($link, $query);

答案 4 :(得分:0)

分辨!!!修复:

简单改变:

mysql_query("INSERT INTO Games VALUES (null,'$gametitle','$gamedescription','$image1url','$categorycode','0','$gametitle2'") or die ("Error Inserting Values into the Games Table");

到:

mysql_query("INSERT INTO Games VALUES (null,'$gametitle','$gamedescription','$image1url','$categorycode','0','$gametitle2')") or die ("Error Inserting Values into the Games Table");

答案 5 :(得分:0)

一个有趣的场景是

<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)";
    }
?>

上面的代码将产生错误,因为在代码进一步进行调用include(database ...)之前会先调用insert_data()函数,因此会出现错误。解决方法是将include(database)移动到函数内部或在调用insert_data查询之前

<?php

    function insert_data(a1,b1)
    {
         include('database_connnection.php');
         Query = "Insert INTO MYTABLE VALUES(a1,b1)";
    }
?>