title: root@localhost can't connect to database through PHP (PDO)
body: <p>I have been trying to connect to my database through PHP for a few hours now, with no success. My PHP code looks like this:</p>
  
  <pre><code>&lt;?php
  
      ini_set('display_errors', 'On');
  
      $hostname = 'localhost';
      $username = 'root';
      $password = '';
  
      function testdb_connect($hostname, $username, $password) {
          $db = new PDO("mysql:host=$hostname;dbname=asdf", $username, $password);
          return $db;
      }
  
      try {
          $db = testdb_connect($hostname, $username, $password);
          echo 'Connected to database';
      } catch(PDOException $e) {
          echo $e-&gt;getMessage();
          }
  
      ?&gt;
  </code></pre>
  
  <p>What I have already tried:</p>
  
  <ul>
  <li>Checked if 'root' is configured to 'localhost' (Y) </li>
  <li>Checked if root@localhost has Grant_priv/Super_priv permissions (Y
  for both)</li>
  <li>Checked if Apache/MySQL is running (Y for both) Checked if my PHP</li>
  <li>Code is wrong, i.e. I am missing something (N)</li>
  </ul>
  
  <p>Any ideas where the Problem could lie? Thanks in advance!</p>
  
  <p>EDIT:</p>
  
  <p>I am so sorry, I have seen the error message so often today, I forgot to mention it here:</p>
  
  <pre><code>SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: NO)
  </code></pre>
  
  <p>RE-EDIT:</p>
  
  <p>Here the php code with the user I created:</p>
  
  <pre><code>&lt;?php
  
      ini_set('display_errors', 'On');
  
      $hostname = 'localhost';
      $username = 'myUserName';
      $password = 'thatUserPassword';
  
      function testdb_connect($hostname, $username, $password) {
          $db = new PDO("mysql:host=$hostname;dbname=asdf", $username, $password);
          return $db;
      }
  
      try {
          $db = testdb_connect($hostname, $username,    $password);
          echo 'Connected to database';
      } catch(PDOException $e) {
          echo $e-&gt;getMessage();
      }
  
      ?&gt;
  </code></pre>
  
  <p>Error Message:</p>
  
  <pre><code>SQLSTATE[HY000] [1045] Access denied for user 'myUserName'@'localhost' (using password: YES)
  </code></pre>