title: Separating database config from connection
body: <p>I'm trying to make a connection to the database while having the database config information saperated from the connection.</p>
  
  <p>This is the code I wrote</p>
  
  <p>db config information:</p>
  
  <pre><code>$config = array(
  "database" =&gt; array(
     "host" =&gt; "host",
     "username" =&gt; "username",
     "password" =&gt; "password",
     "name" =&gt; "name",
  )
  );
  
  define("databse_config", $config["database"]);
  </code></pre>
  
  <p><strong><em>$config</em></strong> normaly also contains other information not relevant to the question ,that is why I have defined "database_config".</p>
  
  <p>database connection:</p>
  
  <pre><code>
  require_once "config.php";
  define("DB_HOST", databse_config['host']);
  define("DB_USERNAME", databse_config['username']);
  define("DB_PASSWORD", databse_config['password']);
  define("DB_NAME", databse_config['name']);
  
  function connect()
  {
      $dbhost = DB_HOST;
      $dbuser = DB_USERNAME;
      $dbpass = DB_PASSWORD;
      $dbname = DB_NAME;
  
      try {
          $conn = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);
          $conn-&gt;exec("set names utf8");
          $conn-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
          return $conn;
      } catch (PDOException $e) {
          echo 'Connection failed: ' . $e-&gt;getMessage();
      }
  }
  
  connect();
  </code></pre>
  
  <p>The problem I keep running in to is that wile trying to execute the function the page keeps loading and eventualy outputs the following error: </p>
  
  <blockquote>
    <p>Connection failed: SQLSTATE[HY000] [2002] Connection timed out</p>
  </blockquote>
  
  <p>I'm fairly new to PDO so I also tried the same while using mysqli, which gave the same result.</p>
  
  <p>Any help would be appreciated.</p>