-
-
Save skhani/5aebd11015881fb3d288 to your computer and use it in GitHub Desktop.
<?php | |
class Db | |
{ | |
private $_connection; | |
private static $_instance; //The single instance | |
private $_host = DB_HOST; | |
private $_username = DB_USER; | |
private $_password = DB_PASSWORD; | |
private $_database = DB_DB; | |
/* | |
Get an instance of the Database | |
@return Instance | |
*/ | |
public static function getInstance() | |
{ | |
if (!self::$_instance) { // If no instance then make one | |
self::$_instance = new self(); | |
} | |
return self::$_instance; | |
} | |
// Constructor | |
private function __construct() | |
{ | |
try { | |
$this->_connection = new \PDO("mysql:host=$this->_host;dbname=$this->_database", $this->_username, $this->_password); | |
/*** echo a message saying we have connected ***/ | |
echo 'Connected to database'; | |
} catch (PDOException $e) { | |
echo $e->getMessage(); | |
} | |
} | |
// Magic method clone is empty to prevent duplication of connection | |
private function __clone() | |
{ | |
} | |
// Get mysql pdo connection | |
public function getConnection() | |
{ | |
return $this->_connection; | |
} | |
} | |
/** | |
* And you can use it as such in a class | |
* */ | |
class Post { | |
public function __construct(){ | |
$db = Db::getInstance(); | |
$this->_dbh = $db->getConnection(); | |
} | |
public function getPosts() | |
{ | |
try { | |
/*** The SQL SELECT statement ***/ | |
$sql = "SELECT * FROM posts"; | |
foreach ($this->_dbh->query($sql) as $row) { | |
var_dump($row); | |
} | |
/*** close the database connection ***/ | |
$this->_dbh = null; | |
} catch (PDOException $e) { | |
echo $e->getMessage(); | |
} | |
} | |
} |
I already created class DB, and my second class is XYZ.
`
/*
-
Mysql database class - only one connection alowed
/
class Configuration {
private $_connection;
private static $_instance; //The single instance
private $_host = "localhost";
private $_username = "root";
private $_password = "";
private $_database = "centreonyx";
/
Get an instance of the Database
@return Instance
*/
public static function getInstance() {
if(!self::$_instance) { // If no instance then make one
self::$_instance = new self();
}
return self::$_instance;
}
// Constructor
private function __construct() {
$this->_connection = new mysqli($this->_host, $this->_username, $this->_password, $this->_database);// Error handling if(mysqli_connect_error()) { trigger_error("Failed to conencto to MySQL: " . mysql_connect_error(), E_USER_ERROR); }
}
// Magic method clone is empty to prevent duplication of connection
private function __clone() { }
// Get mysqli connection
public function getConnection() {
return $this->_connection;
}
}
require_once 'Config/Configuration.php';
class XYZ {
public function _construct()
{
$db = Configuration::getInstance();
$this->mysqli = $db->getConnection();
}
function insertBlogDetails($bcat,$bname,$bdesc,$bdate,$bwritter)
{
/check blog exist/
$sqlToCheckBlog="SELECT * FROM blog_details WHERE Blog_Name='".$bname."'";
$resultQueryCheck=$this->mysqli->query($sqlToCheckBlog);
print_r($resultQueryCheck);
exit();
}
}`
I m using this way.
please help me where i m doing wrong
Hi,
How do I add ? charset=utf8
After using this class to run a DELETE query, how do I see what was deleted or the affected rows?
if i do
$obj = new Post;
$obj->getPosts("CALL getStates");
i get the data but i dont think this is a good way to do it
how can i bring my values ?