2

我不断收到此错误:

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


?>

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

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

4

6 回答 6

5

尝试将包含移动到第一个查询行上方!像这样

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

要做一个 MySQL 请求,你需要先连接。

于 2012-06-03T20:46:40.343 回答
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'(etc) 的字符串文字,而不是您想要的值。

此外,您似乎mysqli正在使用mysql. 我猜想使用其中一个是值得的——它们是两个不同的库。

于 2012-06-03T22:21:46.370 回答
2

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

1) $link = mysqli_connect($host, $username, $password)
2) mysqli_select_db($link, $db_name);
3) $result = mysqli_query($link, $query);
于 2012-06-03T20:47:39.710 回答
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 中使用密码,这是正确的吗?

于 2012-06-03T22:11:24.427 回答
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");
于 2012-06-04T00:44:22.950 回答
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)";
    }
?>

上面的代码将给出错误,因为 insert_data() 函数在调用 include(database...) 之前被调用,因为它在代码的更下方,因此你会得到错误。修复方法是将 include(database) 移动到函数内部或在调用 insert_data 查询之前

<?php

    function insert_data(a1,b1)
    {
         include('database_connnection.php');
         Query = "Insert INTO MYTABLE VALUES(a1,b1)";
    }
?>
于 2020-04-04T15:52:59.883 回答