title: Creating a PDO connection string from PHP class and DBMS techniques
body: <p>Here is my program description. I tried to create a Database connection class that has different method which differ in the way they pull the records from the database. </p>
  
  <p>What I'm trying to do is -- setting the class attributes to the PDO connection parameters and creating a default constructor with those attributes. Whenever I need data, I will call those methods which are supposed to return records.</p>
  
  <p>Question is : I suppose that my connection string for creating the PDO object is right, but It will show error saying <strong>Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [2002] Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)</strong></p>
  
  <p>Can you please help me out to figure out whats really going on ? 
  And also can you please share with me the techniques and ideas to create secure, robust and dynamically accessible database classes or methods? I would highly appreciate. Thanks in advance! </p>
  
  <p><strong>Code :</strong>    </p>
  
  <pre><code>&lt;?php
  class DatabaseConnection {
  
          private $hostname;
          private $dbname;
          private $username;
          private $password;
  
          public function __construct() {
              $this-&gt;hostname = "hostname";
              $this-&gt;dbname = "database";
              $this-&gt;username ="username";
              $this-&gt;password = "password";
          }
  
          function getDataFromTable($query){
              echo $dns;
              $db = new PDO("mysql:host = {$this-&gt;hostname} ; dbname = {$this-&gt;dbname}", $this-&gt;username, $this-&gt;password);
              // stuffs ...
          }
  }
  
  ?&gt;
  </code></pre>