Skip to content

Instantly share code, notes, and snippets.

@arvindsvt
Last active April 24, 2024 11:36
Show Gist options
  • Save arvindsvt/65f5f65ff661c37d1257c85be6ec273c to your computer and use it in GitHub Desktop.
Save arvindsvt/65f5f65ff661c37d1257c85be6ec273c to your computer and use it in GitHub Desktop.
php-database-singleton-method
Class DbConn {
private static $instance;
private $dbConn;
private $mysqli;
private function __construct() {
$this->mysqli = new mysqli("localhost", "root", "", "fetch_category");
if ($this->mysqli->connect_error) {
die($this->mysqli->connect_error);
}
}
/**
*
* @return DbConn
*/
private static function getInstance(){
if (self::$instance == null){
$className = __CLASS__;
self::$instance = new $className;
//self::$instance = new static();
}
return self::$instance;
}
https://stackoverflow.com/questions/21832809/php-singleton-database-connection-pattern
https://hotexamples.com/examples/-/DataBase/singleton/php-database-singleton-method-examples.html
https://www.linkedin.com/pulse/singleton-php-ranush-malsika-loptc/?trk=public_post_main-feed-card_feed-article-content
https://devpress.csdn.net/mysqldb/62fc6d967e6682346618fb61.html
https://forums.phpfreaks.com/topic/293664-mysqli-singleton-class/
<?php
// $servername = "localhost";
// $username = "root";
// $password = "";
// $dbname = "fetch_category";
// $conn = mysqli_connect($servername, $username, $password, $dbname) or die("Connection failed");
class Database {
public static $instance;
private $mysqli,
$query,
$results,
$count ;
public $last_inserted_id = 0;
public static function getInstance() {
if (!self::$instance) {
self::$instance = new Database();
}
return self::$instance;
}
public function __construct() {
$this->mysqli = new mysqli("localhost", "root", "", "fetch_category");
if ($this->mysqli->connect_error) {
die($this->mysqli->connect_error);
}
}
public function query($sql) {
if ($this->query = $this->mysqli->query($sql)) {
if ($this->query->num_rows > 0) {
while ($row = $this->query->fetch_assoc()) {
$this->results[] = $row;
}
}
$this->count = $this->query->num_rows;
}
return $this;
}
public function results() {
return $this->results;
}
public function count() {
return $this->count;
}
public function insertData($sql) {
if ($this->query = $this->mysqli->query($sql)) {
$this->last_inserted_id = $this->mysqli->insert_id;
}
return $this;
}
}
$r = Database::getInstance()->query('SELECT * FROM employees');
foreach ($r->results() as $row) {
echo $row['first_name']. $row['id']. '</br>';
}
echo $r->count();
echo '</br>';
$DB = Database::getInstance()->insertData("INSERT INTO `employees`(`id`, `first_name`, `last_name`, `email`, `phone`, `post`, `avatar`, `created_at`, `Updated_at`)
VALUES ('aaaa','aa','[value-3]','[value-4]','[value-5]','[value-6]','[value-7]','[value-8]','[value-9]');
");
echo $DB->last_inserted_id;
https://stackoverflow.com/questions/4165195/mysql-query-to-get-column-names
https://www.codexworld.com/php-crud-operations-jquery-ajax-mysql/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment