2

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
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.

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.

2
  • 2
    You have left out the actual code where you connect to the database. Since your problem is exactly with connecting to the database, it would be the most likely location of the cause. Commented Jun 3, 2012 at 20:48
  • Remove the @ symbols in front of the MySql calls in your mysql_connect.php file - this is likely masking an error. Commented Jun 3, 2012 at 22:20

6 Answers 6

5

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, but that still did not work. Any more ideas? I've edited the code above to the most current attempt.
Well if you edited mysqli_connect() to mysql_connect() it should work!
2

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.

Also, it seems you are connecting with mysqli but running queries using mysql. I'd guess that it would be worth using one or the other - they're two different libraries.

Comments

2

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?

3 Comments

The single quotes in the mysql_connect call will cause a problem - currently this has a hostname of $db_host rather than localhost (and so on for the other connection values). Remove the quotes to get it working :)
Haha oops, the quotes carried over from his code :P fixed it now though
Actually, My user does have a password. Thats why I am puzzled. Could it be something is wrong with adding the contents to the database itself? Here is what my database looks like. The ID is auto increment, and the View Count is ++ every time a user visits a page on my site.
2

You should establish a connection before running a query:

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

Comments

0

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

2 Comments

I guess my post helped, since you've edited my changes into your question!
he was missing a ) :D
0

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

<?php

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.