title: php singleton database connection, is this code bad practice?
body: <p>I'm trying to create a simple to use singleton class to connect to mysql database and do queries, the code works fine and i haven't had any problems with it, but since I'm new to OOP I'm wondering whether this is bad practice or not.</p>
  
  <p>Here's the class</p>
  
  <pre><code>class Database {
  private $databaseName = 'dbname';
  private $host = 'localhost';
  private $user = 'user';
  private $password = 'pass'; 
  private static $instance; //store the single instance of the database
  
  private function __construct(){
      //This will load only once regardless of how many times the class is called
      $connection = mysql_connect($this-&gt;host, $this-&gt;user, $this-&gt;password) or die (mysql_error());
      $db = mysql_select_db($this-&gt;databaseName, $connection) or die(mysql_error()); 
      echo 'DB initiated&lt;br&gt;';
  }
  
  //this function makes sure there's only 1 instance of the Database class
  public static function getInstance(){
      if(!self::$instance){
          self::$instance = new Database();
      }
      return self::$instance;     
  }
  
  public function connect() { 
      //db connection
  } 
  public function query($query) {
      //queries   
      $sql = mysql_query($query) or die(mysql_error()); 
      return $sql;
  }
  
  public function numrows($query) {
      //count number of rows  
      $sql = $this-&gt;query($query);
      return mysql_num_rows($sql);
  }
  
  
  }
  
  //Intantiate the class
  $database = Database::getInstance();
  </code></pre>
  
  <p>and when i want to use the class I would do:</p>
  
  <pre><code>$query = "SELECT * FROM registrations";
  echo $database-&gt;numrows($query);
  $sql = $database-&gt;query($query);
  </code></pre>