title: PHP Database Connection File
body: <p>I am trying to create a database connection file so that multiple pages will have a constant connection to the database. I am trying to understand why one set of code works as far as linking to the database is concerned, whereas the other set of code fails. Please help me understand.</p>
  
  <p>Here is the code that works:</p>
  
  <pre><code>$db_connect = mysql_connect('host_name', 'user_name', 'user_password');
      if(!$db_connect){
          die('Not connected :' . mysql_error());
      }
  
  $db_select = mysql_select_db("dispatch", $db_connect);
      if(!$db_select){
          die('No database :' . mysql_error());
      }
  </code></pre>
  
  <p>I am able to establish a working connection to the server, as well as selecting a table and running a query.</p>
  
  <p>Here is the code that fails (I want my code to look like this code but I do not understand why this fails to connect to the server.):</p>
  
  <pre><code>$db_host = "host_name";
  $db_user = "user_name";
  $db_pass = "user_password";
  $db_name = "user_table";
  
  $db_connect = mysql_connect("$db_host", "$db_user", "$db_pass") or die ("Could not connect to MySQL");
  </code></pre>
  
  <p>I even tried this code, and it still fails:</p>
  
  <pre><code>$db_host = "host_name";
  $db_user = "user_name";
  $db_pass = "user_password";
  $db_name = "user_table";
  $db_connect = @mysql_connect("$db_host", "$db_user", "db_pass") or die ("Could not connect to MySQL");
  $db_select = @mysql_select_db("$db_name") or die ("Could not connect to database");
  </code></pre>
  
  <p>Please note that I have tried to switch from single quote ( '' ) to double quotes ( " " ) to no avail.  </p>
  
  <p>Please help me understand. Please not ask why I don't just use the code that works.  I want to understand why the second sets of code DO NOT work. </p>
  
  <p>Please also note that this is only my second time using Stack Overflow, and I am still trying to get the hang of it.  Thanks.</p>
  
  <p>Hit me back...</p>