Skip to content

Instantly share code, notes, and snippets.

@sohelamin
Last active August 29, 2015 14:08
Show Gist options
  • Save sohelamin/36b2449ff2f4f5f61abd to your computer and use it in GitHub Desktop.
Save sohelamin/36b2449ff2f4f5f61abd to your computer and use it in GitHub Desktop.
Mysqli Singleton Class
<?php
/**
* Created by AppzCoder.
* User: SOHEL AMIN
* Date: 10/30/2014
* Time: 1:20 AM
* URL: www.appzcoder.com
*/
class Database {
// Database properties
const DATABASE = '<DATABASE NAME>';
const HOST = '<DATABASE HOST>';
const DATABASE_USER = '<USER>';
const DATABASE_PASSWORD = '<PASSWORD>';
private $_mysqli;
public function __construct()
{
$this->_mysqli = new mysqli(self::HOST, self::DATABASE_USER, self::DATABASE_PASSWORD, self::DATABASE);
if ($this->_mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $this->_mysqli->connect_errno . ") " . $this->_mysqli->connect_error;
}
}
public static function init()
{
static $instance = false;
if( ! $instance ) {
$instance = new self();
}
return $instance;
}
public function query($sql)
{
return $this->_mysqli->query($sql);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment