In php database connectivity is a necessary part and with the Latest versions The security and performances are increasing . In past time We was using Mysql_fetch_array() etc. But in php 5 all the mysql_* are deprecated and all these will remove from php 7 . So better to use mysqli_* . Here is a class with OOPS .

class db extends mysqli {
    // single instance of self shared among all instances
    private static $instance = null;
    // db connection config vars
    private $user = "root";
    private $pass = "password";
    private $dbName = "db name";
    private $dbHost = "localhost";
    
    //This method must be static, and must return an instance of the object if the object
 	//does not already exist.
 	public static function getInstance() {
   	if (!self::$instance instanceof self) {
     		self::$instance = new self;
   	}
   		return self::$instance;
 	}
 	// The clone and wakeup methods prevents external instantiation of copies of the Singleton class,
 	// thus eliminating the possibility of duplicate objects.
 	public function __clone() {
   trigger_error('Clone is not allowed.', E_USER_ERROR);
 	}
 	public function __wakeup() {
   	trigger_error('Deserializing is not allowed.', E_USER_ERROR);
 	}
    
    private function __construct() {
    parent::__construct($this->dbHost, $this->user, $this->pass, $this->dbName);
    if (mysqli_connect_error()) {
        exit('Connect Error (' . mysqli_connect_errno() . ') '
                . mysqli_connect_error());
    }
    parent::set_charset('utf-8');
    
   }
}

Now we can get the database instance at any page by db::getInstance() like this

include_once('dbClass.php');
$db=db::getInstance();

We can further add some general functions in this like query() or get_results() like this

public function dbquery($query)
    {
    	if($this->query($query))
		{
			return true;
		}
    	
    }
    public function get_result($query) 
    {
    	$result = $this->query($query);
    	if ($result->num_rows > 0){
        $row = $result->fetch_assoc();
        return $row;
    	} else
        return null;
    	
    	
    }

usage are like

$allposts=db::getInstance()->get_result('select * from tbl_posts');
$query=db::getInstance()->dbquery($query);

Why Getinstance() this implements the Singleton design pattern. It’s forbidding the programmer to create more than one instance of the class. So, if you access your class from anywhere in your code, instead of creating a new class or something like that you just ask to be given the One instance that as been previously created.If no instance exist, it’s creating one else it only return you the front controller wich as been previously created.

http://codingbin.com/wp-content/uploads/2016/02/php_mysqli.pnghttp://codingbin.com/wp-content/uploads/2016/02/php_mysqli-150x150.pngdevidMysqlphpIn php database connectivity is a necessary part and with the Latest versions The security and performances are increasing . In past time We was using Mysql_fetch_array() etc. But in php 5 all the mysql_* are deprecated and all these will remove from php 7 . So better...coding, php, wordpress, yii, cakephp, and  jquery

Are you want to get implementation help, or modify or extend the functionality of this script? Submit paid service request