pdo probleem
ik ben bezig om een pdo class te schrijven en loop tegen het volgende probleem :
Fatal error: Non-static method PDO::query() cannot be called statically in D:\Program Files\devel\xampp\htdocs\Mailchimp\demo\classes\db.class.php on line 189
de line waar het om gaat is de volgende
Code (php)
het probleem doet zich voor op het moment dat ik een select query wil uit voeren via de getArray() functie
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<?php
public static function getArray ($query, $keyCol = false, $valueCol= false) {
try {
$res = self::execQuery($query);
$arr = array();
// fetch array
while ($row = $res->fetch(PDO::FETCH_ASSOC))
{
$arr = $row;
}
}
catch (PDOException $e) {
if(self::$debug_mode)
{
Debug::log_string('Execute query: Query = '.$query.' Error = '.$e->getMessage().' Result = '.count($return));
}
}
if (!$keyCol) {
$arr[] = $geg;
} elseif (!$keyCol && $valueCol) {
$arr[] = $geg[$valueCol];
} elseif ($keyCol && $valueCol) {
$arr[$geg][$keyCol] = $geg[$valueCol];
} else {
$arr[$geg][$keyCol] = $geg;
}
return $arr;
}
?>
public static function getArray ($query, $keyCol = false, $valueCol= false) {
try {
$res = self::execQuery($query);
$arr = array();
// fetch array
while ($row = $res->fetch(PDO::FETCH_ASSOC))
{
$arr = $row;
}
}
catch (PDOException $e) {
if(self::$debug_mode)
{
Debug::log_string('Execute query: Query = '.$query.' Error = '.$e->getMessage().' Result = '.count($return));
}
}
if (!$keyCol) {
$arr[] = $geg;
} elseif (!$keyCol && $valueCol) {
$arr[] = $geg[$valueCol];
} elseif ($keyCol && $valueCol) {
$arr[$geg][$keyCol] = $geg[$valueCol];
} else {
$arr[$geg][$keyCol] = $geg;
}
return $arr;
}
?>
iemand suggesties
Een niet static function roep je op met
En niet zoals jij dat doet
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<?php
class db {
public $db;
public $query;
private $server;
private $database;
private $username;
private $password;
function __construct ($database ,$tableName) {
$this->tableName = $tableName;
$this->fieldList = array();
$this->queryType = "insert";
$this->configArr = array();
$this->server = 'localhost';
$this->database = $database;
$this->username = 'test';
$this->password = 'AdsFUJfBzSU2jFrZ';
}
// deze connect wordt nu nog gebruik wil gaan kijken of deze alleen kan worden uitgevoerd
// wanneer de class wordt aangeroepen
// standaard PDOconnect met als toevoeging een debug tool ($debug staat nu op true)
public function connect () {
try {
$this->db = new PDO("mysql:host=".$this->server."; dbname=".$this->database, $this->username, $this->password);
}
catch (PDOException $e) {
echo 'connection mysql : Error='.$e->getMessage();
die();
}
}
public function getArray ($query, $keyCol = false, $valueCol= false) {
try {
$res = db->execQuery($query);
$arr = array();
// fetch array
while ($row = $res->fetch(PDO::FETCH_ASSOC))
{
$arr = $row;
}
}
catch (PDOException $e) {
if(self::$debug_mode)
{
Debug::log_string('Execute query: Query = '.$query.' Error = '.$e->getMessage().' Result = '.count($return));
}
}
if (!$keyCol) {
$arr[] = $geg;
} elseif (!$keyCol && $valueCol) {
$arr[] = $geg[$valueCol];
} elseif ($keyCol && $valueCol) {
$arr[$geg][$keyCol] = $geg[$valueCol];
} else {
$arr[$geg][$keyCol] = $geg;
}
return $arr;
}
// deze functie moet uitgebreid getest worden
public function getSingleResult ($query, $fieldName = false) {
$res = self::execQuery($query);
$geg = $res->fetchColumn();
if($fieldName) {
return $geg[$fieldName];
}else{
return $geg;
}
}
public function quickNumRows($query){
$res = self::execQuery($query);
return $res->rowCount();
}
public function quickFetch($query, $keys = false, $values = false) {
try {
$res = self::execQuery($query);
}
catch (PDOException $e) {
if(self::$debug_mode)
{
Debug::log_string('Execute query: Query = '.$query.' Error = '.$e->getMessage().' Result = '.count($return));
}
$retVal = array();
if($res->rowCount() == 1 && !$keys && !$values) {
$retVal = $res->fetch(PDO::FETCH_ASSOC);
}elseif($res -> rowCount() == 0){
return false;
}elseif($res->rowCount() > 1 || $keys || $values ) {
while($geg->fetch(PDO::FETCH_ASSOC)){
if(!keys && !$values) {
$retVal = $geg;
}elseif($keys && !$values) {
$retVal[$geg][$keys] = $geg;
}elseif($keys && $values) {
$retVal[$geg][$keys] = $geg[$values];
}elseif(!$keys && $values) {
$retVal = $geg[$values];
}
}
return $retVal;
}
}
}
public function stripHtml($str) {
return htmlentities($str);
}
function stripSlashesRecursive($array) {
foreach($array as $key=>$value) {
if(is_array($value)) {
$value=$this->stripSlashesRecursive($value);
$array_temp[$key]=$value;
} else {
$array_temp[$key]=stripslashes($value);
}
}
return $array_temp;
}
public function addField($fieldName, $fieldValue) {
if ($this->configArr[stripHtml]) {
$fieldValue = $this->stripHtml($fieldValue);
}
if ($this->configArr[stripSlashesRecursive]) {
if (is_array($fieldValue)) {
$fieldValue = base64_encode(serialize($this->stripSlashesRecursive($fieldValue)));
}
} else {
if (is_array($fieldValue)) {
$fieldValue = base64_encode(serialize($fieldValue));
}
}
$this->fieldList[$fieldName] = $fieldValue;
}
function setConfig($type) {
$this->configArr[$type] = true;
}
public function insert () {
$insertQuery = "INSERT INTO ".$this->tableName." (";
$keyArr = array();
$valueArr = array();
$statementArr = array();
foreach($this->fieldList as $key => $value) {
$keyArr[] = $key;
$valueArr[] = "'".$value."'";
$statementArr[] ="':".$key."'";
}
$insertQuery .= join(", ", $keyArr);
$insertQuery .=") VALUES (";
$insertQuery .= join(", ", $statementArr);
$insertQuery .=")";
//placeholder prepare.
//$sth = $dbh->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
$prep = $this->db->prepare($insertQuery);
$I = 0;
foreach($statementArr as $statement) {
}
$this->query = $insertQuery;
echo $insertQuery;
}
// query uitvoeren tijdelijk mssn voor altijd.
public function execQuery($q){
var_dump($this->query);die();
if($q) {
$res = PDO->query($q);
}else{
$res = PDO->query($this->query);
}
return $res;
}
}
?>
class db {
public $db;
public $query;
private $server;
private $database;
private $username;
private $password;
function __construct ($database ,$tableName) {
$this->tableName = $tableName;
$this->fieldList = array();
$this->queryType = "insert";
$this->configArr = array();
$this->server = 'localhost';
$this->database = $database;
$this->username = 'test';
$this->password = 'AdsFUJfBzSU2jFrZ';
}
// deze connect wordt nu nog gebruik wil gaan kijken of deze alleen kan worden uitgevoerd
// wanneer de class wordt aangeroepen
// standaard PDOconnect met als toevoeging een debug tool ($debug staat nu op true)
public function connect () {
try {
$this->db = new PDO("mysql:host=".$this->server."; dbname=".$this->database, $this->username, $this->password);
}
catch (PDOException $e) {
echo 'connection mysql : Error='.$e->getMessage();
die();
}
}
public function getArray ($query, $keyCol = false, $valueCol= false) {
try {
$res = db->execQuery($query);
$arr = array();
// fetch array
while ($row = $res->fetch(PDO::FETCH_ASSOC))
{
$arr = $row;
}
}
catch (PDOException $e) {
if(self::$debug_mode)
{
Debug::log_string('Execute query: Query = '.$query.' Error = '.$e->getMessage().' Result = '.count($return));
}
}
if (!$keyCol) {
$arr[] = $geg;
} elseif (!$keyCol && $valueCol) {
$arr[] = $geg[$valueCol];
} elseif ($keyCol && $valueCol) {
$arr[$geg][$keyCol] = $geg[$valueCol];
} else {
$arr[$geg][$keyCol] = $geg;
}
return $arr;
}
// deze functie moet uitgebreid getest worden
public function getSingleResult ($query, $fieldName = false) {
$res = self::execQuery($query);
$geg = $res->fetchColumn();
if($fieldName) {
return $geg[$fieldName];
}else{
return $geg;
}
}
public function quickNumRows($query){
$res = self::execQuery($query);
return $res->rowCount();
}
public function quickFetch($query, $keys = false, $values = false) {
try {
$res = self::execQuery($query);
}
catch (PDOException $e) {
if(self::$debug_mode)
{
Debug::log_string('Execute query: Query = '.$query.' Error = '.$e->getMessage().' Result = '.count($return));
}
$retVal = array();
if($res->rowCount() == 1 && !$keys && !$values) {
$retVal = $res->fetch(PDO::FETCH_ASSOC);
}elseif($res -> rowCount() == 0){
return false;
}elseif($res->rowCount() > 1 || $keys || $values ) {
while($geg->fetch(PDO::FETCH_ASSOC)){
if(!keys && !$values) {
$retVal = $geg;
}elseif($keys && !$values) {
$retVal[$geg][$keys] = $geg;
}elseif($keys && $values) {
$retVal[$geg][$keys] = $geg[$values];
}elseif(!$keys && $values) {
$retVal = $geg[$values];
}
}
return $retVal;
}
}
}
public function stripHtml($str) {
return htmlentities($str);
}
function stripSlashesRecursive($array) {
foreach($array as $key=>$value) {
if(is_array($value)) {
$value=$this->stripSlashesRecursive($value);
$array_temp[$key]=$value;
} else {
$array_temp[$key]=stripslashes($value);
}
}
return $array_temp;
}
public function addField($fieldName, $fieldValue) {
if ($this->configArr[stripHtml]) {
$fieldValue = $this->stripHtml($fieldValue);
}
if ($this->configArr[stripSlashesRecursive]) {
if (is_array($fieldValue)) {
$fieldValue = base64_encode(serialize($this->stripSlashesRecursive($fieldValue)));
}
} else {
if (is_array($fieldValue)) {
$fieldValue = base64_encode(serialize($fieldValue));
}
}
$this->fieldList[$fieldName] = $fieldValue;
}
function setConfig($type) {
$this->configArr[$type] = true;
}
public function insert () {
$insertQuery = "INSERT INTO ".$this->tableName." (";
$keyArr = array();
$valueArr = array();
$statementArr = array();
foreach($this->fieldList as $key => $value) {
$keyArr[] = $key;
$valueArr[] = "'".$value."'";
$statementArr[] ="':".$key."'";
}
$insertQuery .= join(", ", $keyArr);
$insertQuery .=") VALUES (";
$insertQuery .= join(", ", $statementArr);
$insertQuery .=")";
//placeholder prepare.
//$sth = $dbh->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
$prep = $this->db->prepare($insertQuery);
$I = 0;
foreach($statementArr as $statement) {
}
$this->query = $insertQuery;
echo $insertQuery;
}
// query uitvoeren tijdelijk mssn voor altijd.
public function execQuery($q){
var_dump($this->query);die();
if($q) {
$res = PDO->query($q);
}else{
$res = PDO->query($this->query);
}
return $res;
}
}
?>
Dat zal je inderdaad ook nooit werkend krijgen, want de query method binnen de PDO class is niet static. Die kan je dus simpelweg niet als een static functie aanroepen. Einde oefening.
was ook bezig in mijn oude class dus weer wat geleerd maak een map met old