Last active
November 8, 2022 09:45
-
-
Save skhani/5aebd11015881fb3d288 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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(); | |
} | |
} | |
} |
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?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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);
}
// 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