Home » Questions » Computers [ Ask a new question ]

MySQL/Apache Error in PHP MySQL query

MySQL/Apache Error in PHP MySQL query

"I am getting the following error:

Access denied for user 'apache'@'localhost' (using password: NO)

When using the following code:

<?php

include(""../includes/connect.php"");

$query = ""SELECT * from story"";

$result = mysql_query($query) or die(mysql_error());

echo ""<h1>Delete Story</h1>"";

if (mysql_num_rows($result) > 0) {
while($row = mysql_fetch_row($result)){
echo '<b>'.$row[1].'</b><span align=""right""><a href=""../process/delete_story.php?id='.$row[0].'"">Delete</a></span>';
echo '<br /><i>'.$row[2].'</i>';
}
}
else {
echo ""No stories available."";
}
?>

The connect.php file contains my MySQL connect calls that are working fine with my INSERT queries in another portion of the software. If I comment out the $result = mysql_query line, then it goes through to the else statement. So, it is that line or the content in the if.

I have been searching the net for any solutions, and most seem to be related to too many MySQL connections or that the user I am logging into MySQL as does not have permission. I have checked both. I can still perform my other queries elsewhere in the software, and I have verified that the account has the correct permissions."

Asked by: Guest | Views: 385
Total answers/comments: 4
Guest [Entry]

"And if it matters at all, apache@localhost is not the name of the user account that I use to get into the database. I don't have any user accounts with the name apache in them at all for that matter.

If it is saying 'apache@localhost' the username is not getting passed correctly to the MySQL connection. 'apache' is normally the user that runs the httpd process (at least on Redhat-based systems) and if no username is passed during the connection MySQL uses whomever is calling for the connection.

If you do the connection right in your script, not in a called file, do you get the same error?"
Guest [Entry]

Change the include() to require(). If the "connect.php" file can't be require()d, the script will fail with a fatal error, whereas include() only generates a warning. If the username you're passing to mysql_connect() isn't "apache", an incorrect path to the connect script is the most common way to get this type of error.
Guest [Entry]

"Don't forget to check your database error logs. You should be able to see if you are even hitting the DB. If you aren't, you should check your firewall rules on the box. On a linux box you can run iptables -L to get the firewall list rules.

Otherwise it will be a pure access issue. Do a ""select * from mysql.user"" to see if the apache user is even set up in there. Further, I would recommend creating an account specifically for your app as opposed to using apache, since any other app you create will run as apache by default, and could get unauthorized access to your db.

Just look up ""GRANT"" in the documentation @ dev.mysql.com to get more info. If you have more specific questiosn regarding db, just edit your question, and i will take a look."
Guest [Entry]

"Does the connect.php script actually make the connection or does it just define a function you need to call to create a connection? The error you're getting is symptomatic of not having a previously established connection at all.

ETA: Also change the include to a require. I suspect it's not actually including the file at all. But include can fail silently."