<p>I am getting the following error:</p>

<blockquote>
  <p>Access denied for user 'apache'@'localhost' (using password: NO)</p>
</blockquote>

<p>When using the following code:</p>

<pre><code>&lt;?php

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

$query = "SELECT * from story";

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

echo "&lt;h1&gt;Delete Story&lt;/h1&gt;";

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

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

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

[{'answer_id': 2908, 'author': 'Teifion', 'author_id': 1384652, 'author_profile': 'https://Stackoverflow.com/users/1384652', 'pm_score': 1, 'selected': False, 'text': '<p>Just to check, if you use <strong>just</strong> this part you get an error?</p>\n\n<pre><code>&lt;?php\ninclude("../includes/connect.php");\n\n$query = "SELECT * from story";\n$result = mysql_query($query) or die(mysql_error());\n</code></pre>\n\n<p>If so, do you still get an error if you copy and paste one of those Inserts into this page, I am trying to see if it\'s local to the page or that actual line.</p>\n\n<p>Also, can you post a copy of the connection calls (minus passwords), unless the inserts use exactly the same syntax as this example.</p>\n'}, {'answer_id': 2911, 'author': 'Justin Bennett', 'author_id': 271, 'author_profile': 'https://Stackoverflow.com/users/271', 'pm_score': 1, 'selected': False, 'text': '<p>Does the apache user require a password to connect to the database?  If so, then the fact that it says "using password: NO" would lead me to believe that the code is trying to connect without a password.</p>\n\n<p>If, however, the apache user doesn\'t require a password, a double-check of the permissions may be a good idea (which you mentioned you already checked).  It may still be beneficial to try executing something like this at a mysql prompt:</p>\n\n<pre><code>GRANT ALL PRIVILEGES ON `*databasename*`.* to \'apache\'@\'localhost\';\n</code></pre>\n\n<p>That syntax should be correct.  </p>\n\n<p>Other than that, I\'m just as stumped as you are.</p>\n'}, {'answer_id': 2915, 'author': 'Ecton', 'author_id': 457, 'author_profile': 'https://Stackoverflow.com/users/457', 'pm_score': 1, 'selected': False, 'text': '<p>If indeed you are able to insert using the same connection calls, your problem most likely lies in the user &quot;apache&quot; not having SELECT permissions on the database. If you have phpMyAdmin installed you can look at the permissions for the user in the Privileges pane. phpMyAdmin also makes it very easy to modify the permissions.</p>\n<p>If you only have access to the command line, you can check the permissions from the mysql database.</p>\n<p>You\'ll probably need to do something like:</p>\n<pre class="lang-sql prettyprint-override"><code>GRANT SELECT ON myDatabase.myTable TO \'apache\'@\'localhost\';\n</code></pre>\n'}, {'answer_id': 2943, 'author': 'Mesidin', 'author_id': 454, 'author_profile': 'https://Stackoverflow.com/users/454', 'pm_score': 1, 'selected': False, 'text': '<blockquote>\n  <p>Just to check, if you use just this part you get an error?</p>\n  \n  <p>If so, do you still get an error if you copy and paste one of those Inserts into this >page, I am trying to see if it\'s local to the page or that actual line.</p>\n  \n  <p>Also, can you post a copy of the connection calls (minus passwords), unless the inserts >use exactly the same syntax as this example.</p>\n</blockquote>\n\n<p>Here is what is in the connection.php file.  I linked to the file through an include in the same fashion as where I execute the INSERT queries elsewhere in the code.</p>\n\n<pre><code>$conn = mysql_connect("localhost", ******, ******) or die("Could not connect");\nmysql_select_db("adbay_com_-_cms") or die("Could not select database");\n</code></pre>\n\n<p>I will try the working INSERT query in this area to check that out.</p>\n\n<p>As to the others posting about the password access.  I did, as stated in my first posting, check permissions.  I used phpMyAdmin to verify that the permissions for the user account I was using were correct.  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.</p>\n'}, {'answer_id': 2975, 'author': 'dragonmantank', 'author_id': 204, 'author_profile': 'https://Stackoverflow.com/users/204', 'pm_score': 5, 'selected': True, 'text': "<blockquote>\n  <p>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.</p>\n</blockquote>\n\n<p>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.</p>\n\n<p>If you do the connection right in your script, not in a called file, do you get the same error?</p>\n"}, {'answer_id': 222793, 'author': 'Zak', 'author_id': 2112692, 'author_profile': 'https://Stackoverflow.com/users/2112692', 'pm_score': 2, 'selected': False, 'text': '<p>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. </p>\n\n<p>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. </p>\n\n<p>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.</p>\n'}, {'answer_id': 222810, 'author': 'KernelM', 'author_id': 22328, 'author_profile': 'https://Stackoverflow.com/users/22328', 'pm_score': 2, 'selected': False, 'text': "<p>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.</p>\n\n<p>ETA: Also change the include to a require. I suspect it's not actually including the file at all. But include can fail silently.</p>\n"}, {'answer_id': 222946, 'author': 'Piskvor left the building', 'author_id': 19746, 'author_profile': 'https://Stackoverflow.com/users/19746', 'pm_score': 2, 'selected': False, 'text': '<p>Change the include() to require(). <strong>If the "connect.php" file can\'t be require()d, the script will fail</strong> with a fatal error, whereas <strong>include() only generates a warning</strong>. 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.</p>\n'}, {'answer_id': 603700, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 2, 'selected': False, 'text': '<p>Dude the answer is a big DUH! which unfortunately it took me a while to figure out as well. You probably have a function like dbconnect() and you are using variables from an include file to make the connection. $conn = mysql_connect($dbhost, $dbuser, $dbpass).</p>\n\n<p>Well since this is inside a function the variables from the include file need to be passed to the function or else the function will not know what $dbhost, $dbuser and $dbpass is. A way to fix this is to make those variables global so your functions can pick them up. Another solution which is not very secure would be to write out you host, user and pass in the mysql_connect function.</p>\n\n<p>Hope this helps but I had the same problem.</p>\n'}, {'answer_id': 1024750, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 1, 'selected': False, 'text': '<p>You can do one of the following:</p>\n\n<ul>\n<li>Add the user "apache" and setup its privileges from phpmyadmin or using mysql on a shell</li>\n<li>Tell php to run <code>mysql_connect</code> as another user, someone who already has the privileges needed (but maybe not root), look for mysql.default_user in your php.ini file.</li>\n</ul>\n'}, {'answer_id': 18892554, 'author': 'MoonJoose', 'author_id': 2795056, 'author_profile': 'https://Stackoverflow.com/users/2795056', 'pm_score': 1, 'selected': False, 'text': "<p>Did you remember to do:</p>\n\n<pre><code>flush privileges;\n</code></pre>\n\n<p>If the user is not set up then it will give the 'apache'@'localhost' error.</p>\n"}]
['https://Stackoverflow.com/questions/2900', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/454/']