php - Singleton database class method -


i'm trying write query() method singleton database class. have ain't flying yet , looking inspiration.

<?php class database {  static private $instance = null;  protected $mysqli;   static public function getinstance()  {      if (null === self::$instance) {          self::$instance = new self;      }      return self::$instance;  }   private function __construct(){         if(!$this->mysqli = new mysqli('localhost','user','pass','db'))             throw new exception('error - database connection');  }   public function query($query) {     while ($this->mysqli->query($query))     {         $row = $this->mysqli->fetch_assoc();     }     return $row; // loop through array  }   private function __clone(){}  } ?> 

i'm not entirely sure if i'm going down correct road. mysqli work in context? instance mysqli class has query() method part of class. i'm introducing namespace clashes here?

my query method handle sort of sql statement ie.

example client code:

$db = database::getinstance(); $db->query("select * t"); $db->query("delete t id=2"); $db->query("update u set col = 'random' id=4"); 


Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

VBA function to include CDATA -