Hi All, I am trying to add insert/update and delete to an OOP singleton database connection using public functions like this, not sure if it is correct?
example code
public function insert($sql){
if($this->_execute= $this->_mysqli->_execute($sql)){
while ($row = $this->_execute->fetch_object()){
$this->_results[] = $row;
}
$this->_count = $this->_execute->num_rows;
}
return $this;
}
//page index.php
<?php
require_once 'classes/db.php';
function Users(){
$users = DB::getInstance()->query('SELECT UserID, UserFullName, UserEmail, UserLastPassword, UserName, UserActive FROM Users');
if ($users->count()){
foreach ($users->results() as $user){
echo $user->UserName, $user->UserActive,$user->UserLastPassword,'<br />';
}
}
}
Users();
//page db.php
<?php
class DB
{
private $_mysqli,
$_query,
$_results = array(),
$_count=0,
$_last_id = -1;
public static $instance;
public static function getInstance(){
if (!isset(self::$instance)){
self::$instance = new DB();
}
return self::$instance;
}
public function __construct() {
$this->_mysqli = new mysqli('localhost','root','','db301847341');
if($this->_mysqli->connect_error) {
die($this->_mysqli->connect_error);
}
}
public function query($sql){
if($this->_query= $this->_mysqli->query($sql)){
while ($row = $this->_query->fetch_object()){
$this->_results[] = $row;
}
$this->_count = $this->_query->num_rows;
}
return $this;
}
public function execute($sql){
if($this->_execute= $this->_mysqli->_execute($sql)){
while ($row = $this->_execute->fetch_object()){
$this->_results[] = $row;
}
$this->_count = $this->_execute->num_rows;
}
return $this;
}
public function results(){
return $this->_results;
}
public function count(){
return $this->_count;
}
}