Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit fe40f06

Browse files
refactor: Updated DB object to be a non-static object
1 parent 8c5a5d3 commit fe40f06

File tree

2 files changed

+34
-86
lines changed

2 files changed

+34
-86
lines changed

‎db.class.php

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,11 @@
1010
* 2021年11月16日 -> Fixed Fetch method when same SQL is called more than once
1111
* 2022年01月22日 -> Fixed Fetch method when same SQL is called in a simple way. Also added a way to RETRIEVE data if same SQL is sent again
1212
* 2022年03月30日 -> Made some refactor in try/catch methods. Changed explanations to methods
13+
* 2022年04月04日 -> Turned DB Objet into non-static object. This way is much more easier to run multiple queries while fetching more data
1314
*/
1415

1516
class db
1617
{
17-
/** This will define if errors will be displayed */
18-
private $debug = false;
19-
2018
/** This stores the current $connectionName */
2119
protected static $connectionName = null;
2220

@@ -63,11 +61,14 @@ class db
6361
/**
6462
* @return object $instance Returns the database instance
6563
*/
66-
private static function getInstance()
64+
private static function getInstance($key = false)
6765
{
6866
if (self::$dbInit == null) {
6967
self::$dbInit = microtime();
7068
}
69+
if ($key && isset(self::$object[$key])) {
70+
return self::$object[$key]->getInstance();
71+
}
7172
if (is_null(self::$connectionName)) {
7273
global $DB_HOST, $DB_USER, $DB_PASSWORD, $DB_NAME;
7374
$connectionName = "default";
@@ -84,7 +85,6 @@ private static function getInstance()
8485
$instance = new PDO('mysql:host=' . self::$connections[self::$connectionName]['HOST'] . ";dbname=" . self::$connections[self::$connectionName]['NAME'] . ";", self::$connections[self::$connectionName]['USER'], self::$connections[self::$connectionName]['PASSWORD']);
8586
if ($instance) {
8687
$instance->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
87-
//$connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
8888
self::updateTotalRequests();
8989
return $instance;
9090
} else {
@@ -175,10 +175,6 @@ private static function encapsulate($mixed, $simple = false)
175175
unset(self::$object[$key]);
176176
return false;
177177
}
178-
/*if (self::$object[$key]->extra["rows"] + 1 == self::$object[$key]->extra["totalEntries"] && self::$object[$key]->extra["totalEntries"] <= 0) {
179-
unset(self::$object[$key]);
180-
return false;
181-
}*/
182178
return self::$object[$key];
183179
}
184180
if ($mixed instanceof dbObject) {
@@ -205,7 +201,6 @@ public static function fetch($mixed, $simple = false)
205201
}
206202
$mixed = self::encapsulate($mixed, $simple);
207203
return is_bool($mixed) ? $mixed : $mixed->getData();
208-
//return ($mixed ? $mixed->getData() : $mixed);
209204
}
210205
if ($mixed instanceof dbObject) {
211206
return $mixed->getData();
@@ -647,7 +642,7 @@ public static function prepare($instance, $sql)
647642
*/
648643
public static function set(&$instance, $key, $value)
649644
{
650-
$instance->bindValue(":" . $key, $value);
645+
$instance->bindValue(":" . $key, is_array($value) ? json_encode($value) : $value);
651646
}
652647

653648
/**
@@ -711,11 +706,14 @@ public static function insert($data, $table, $additional = array())
711706
self::set($stmnt, $key, $value);
712707
}
713708
try {
714-
$stmnt->execute();
715-
self::updateTotalRequests();
716-
self::$id = $instance->lastInsertId();
717-
unset($stmnt, $instance);
718-
return true;
709+
if (is_object($stmnt)) {
710+
$stmnt->execute();
711+
self::updateTotalRequests();
712+
self::$id = $instance->lastInsertId();
713+
unset($stmnt, $instance);
714+
return true;
715+
}
716+
return false;
719717
} catch (Error $e) {
720718
throw ("An error ocurred: " . $e);
721719
return false;
@@ -766,9 +764,12 @@ public static function update($data, $table, $rules = array(), $additional = arr
766764
}
767765
}
768766
try {
769-
$stmnt->execute();
770-
self::updateTotalRequests();
771-
return true;
767+
if (is_object($stmnt)) {
768+
$stmnt->execute();
769+
self::updateTotalRequests();
770+
return true;
771+
}
772+
return false;
772773
} catch (Error $e) {
773774
throw ("An error has occured: " . $e);
774775
return false;
@@ -797,7 +798,10 @@ public static function delete($table, $rules = array())
797798
}
798799
}
799800
try {
800-
return $stmnt->execute();
801+
if (is_object($stmnt)) {
802+
return $stmnt->execute();
803+
}
804+
return false;
801805
} catch (Error $e) {
802806
throw ("An error has occured: " . $e);
803807
return false;
@@ -884,7 +888,7 @@ public static function URLNormalize($string)
884888
class dbObject
885889
{
886890
/** It is the DB instance */
887-
protected static$instance = null;
891+
protected $instance = null;
888892

889893
private $data = array();
890894

@@ -908,17 +912,17 @@ public function __construct($instance, $extra = array())
908912
/**
909913
* @param object Set as reference to DB Class to work with
910914
*/
911-
private staticfunction setInstance($instance)
915+
private function setInstance($instance)
912916
{
913-
self::$instance = $instance;
917+
$this->instance = $instance;
914918
}
915919

916920
/**
917921
* @return object Returns DB Class intance reference
918922
*/
919-
public staticfunction getInstance()
923+
public function getInstance()
920924
{
921-
return self::$instance;
925+
return $this->instance;
922926
}
923927

924928
/**

‎index.php

Lines changed: 5 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -7,69 +7,13 @@
77

88
db::addConnection('default', array("HOST" => "localhost", "USER" => "root", "PASSWORD" => "", "NAME" => "test"));
99

10-
$emails = db::fetchAll("describe emails");
11-
echo "<pre>";
12-
print_R($emails);
13-
echo "</pre>";
14-
echo "<hr/>";
15-
16-
while ($dado = db::fetch("describe emails")) {
10+
while ($dado = db::fetch("SELECT * FROM emails")) {
11+
1712
echo "<pre>";
1813
print_r($dado);
14+
$lista = db::fetch("SELECT * FROM listas");
15+
print_r($lista);
1916
echo "</pre>";
2017
}
2118

22-
$emails = db::fetchAll("describe emails");
23-
echo "<pre>";
24-
print_R($emails);
25-
echo "</pre>";
26-
echo "<hr/>";
27-
$emails = db::fetchAll("describe emails");
28-
echo "<pre>";
29-
print_R($emails);
30-
echo "</pre>";
31-
echo "<hr/>";
32-
$emails = db::fetchAll("describe emails");
33-
echo "<pre>";
34-
print_R($emails);
35-
echo "</pre>";
36-
echo "<hr/>";
37-
$emails = db::fetchAll("describe emails");
38-
echo "<pre>";
39-
print_R($emails);
40-
echo "</pre>";
41-
echo "<hr/>";
42-
$emails = db::fetchAll("describe emails");
43-
echo "<pre>";
44-
print_R($emails);
45-
echo "</pre>";
46-
echo "<hr/>";
47-
$emails = db::fetchAll("describe emails");
48-
echo "<pre>";
49-
print_R($emails);
50-
echo "</pre>";
51-
echo "<hr/>";
52-
$emails = db::fetchAll("describe emails");
53-
echo "<pre>";
54-
print_R($emails);
55-
echo "</pre>";
56-
echo "<hr/>";
57-
$emails = db::fetchAll("describe emails");
58-
echo "<pre>";
59-
print_R($emails);
60-
echo "</pre>";
61-
echo "<hr/>";
62-
$emails = db::fetchAll("describe emails");
63-
echo "<pre>";
64-
print_R($emails);
65-
echo "</pre>";
66-
echo "<hr/>";
67-
$emails = db::fetchAll("describe emails");
68-
echo "<pre>";
69-
print_R($emails);
70-
echo "</pre>";
71-
echo "<hr/>";
72-
73-
echo "<br/><br/>Oi<br/><br/>";
74-
$check = db::fetch("describe emails");
75-
print_r($check);
19+
db::performance();

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /