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 b2b9d2e

Browse files
refactor: Fixed (again) fetch method... two times in a row
1 parent 6bb7f13 commit b2b9d2e

File tree

2 files changed

+84
-10
lines changed

2 files changed

+84
-10
lines changed

‎db.class.php

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
* 2021年08月17日 -> Made a few improvements within base core functions
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
12+
* 2022年01月24日 -> Fetch method is getting more complicated to solve. Third time in a row.
1213
*/
1314

1415
class db
@@ -139,10 +140,14 @@ private static function encapsulate($mixed, $simple = false)
139140
$instance = new dbObject(self::getInstance()->query($mixed), array("key" => $key, "simple" => $simple));
140141
self::$object[$key] = $instance;
141142
}
142-
if (self::$object[$key]->extra["rows"] + 1== self::$object[$key]->extra["totalEntries"] && self::$object[$key]->extra["totalEntries"] <= 0) {
143+
if (self::$object[$key]== "unsetted") {
143144
unset(self::$object[$key]);
144145
return false;
145146
}
147+
/*if (self::$object[$key]->extra["rows"] + 1 == self::$object[$key]->extra["totalEntries"] && self::$object[$key]->extra["totalEntries"] <= 0) {
148+
unset(self::$object[$key]);
149+
return false;
150+
}*/
146151
return self::$object[$key];
147152
}
148153
if ($mixed instanceof dbObject) {
@@ -187,6 +192,12 @@ public static function fetchAll($mixed)
187192
}
188193
}
189194

195+
public static function unsetObject($object)
196+
{
197+
self::$object[$object->extra['key']] = "unsetted";
198+
unset(self::$object[$object->extra['key']]);
199+
}
200+
190201
/** It counts the total rows that $mixed have */
191202
public static function count($mixed)
192203
{
@@ -413,10 +424,11 @@ public static function page($echo = true, $class = "")
413424
$buttons[] = str_replace(array("{rel}", "{target}", '{text|number}', '{active}', '{disabled}'), array("rel='next'", $url . "?" . http_build_query($_GET), $words["next"], '', ''), $htmlButton);
414425
}
415426
}
427+
$return = str_replace(array("{additional}", "{buttons}"), array($words['class'], implode("", $buttons)), $htmlWrapper);
416428
if ($echo) {
417-
echo str_replace(array("{additional}", "{buttons}"), array($words['class'], implode("", $buttons)), $htmlWrapper);
429+
echo $return;
418430
} else {
419-
return true;
431+
return $return;
420432
}
421433
}
422434
}
@@ -470,7 +482,7 @@ public static function setFriendlyURL($FriendlyURLInstance)
470482
/** It will retrieve the collumns of the given $table */
471483
private static function getTableCollumns($table)
472484
{
473-
return self::fetchAll(self::query("DESCRIBE $table"));
485+
return self::fetchAll("DESCRIBE $table;");
474486
}
475487

476488
/** It will fix invalid $data keys before insert them on the $table. It removes not used keys inside $data, and bind a empty string if a specific $key that exists on the table, doesn't exist on $data */
@@ -766,13 +778,21 @@ public static function getInstance()
766778
public function getData($all = false)
767779
{
768780
if ($all) {
769-
return $this->getInstance()->fetchAll(PDO::FETCH_ASSOC);
781+
$data = $this->getInstance()->fetchAll(PDO::FETCH_ASSOC);
782+
db::unsetObject($this);
783+
return $data;
770784
}
771785
if (!$this->extra['simple']) {
772786
$this->extra["rows"]++;
773787
$data = $this->getInstance()->fetch(PDO::FETCH_ASSOC);
788+
if ($this->extra['rows'] == $this->extra['totalEntries']) {
789+
db::unsetObject($this);
790+
}
774791
return $data;
775792
} else {
793+
if (empty($this->data)) {
794+
return false;
795+
}
776796
return $this->data[0];
777797
}
778798
}

‎index.php

Lines changed: 59 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,69 @@
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/>";
1015

11-
12-
while ($dado = db::fetch("select * from emails")) {
16+
while ($dado = db::fetch("describe emails")) {
1317
echo "<pre>";
1418
print_r($dado);
1519
echo "</pre>";
1620
}
1721

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/>";
1872

19-
$check = db::fetch("select * from emails");
20-
echo"q";
21-
print_r($check);
73+
echo"<br/><br/>Oi<br/><br/>";
74+
$check = db::fetch("describe emails");
75+
print_r($check);

0 commit comments

Comments
(0)

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