As mentioned there’s a bug in the db script. This is how I got there
looked at the $result variable directly after the query was run.
It showed this with >= 1
array(1) {
[0] =>
bool(true)
}
array(1) {
[0] =>
bool(true)
}
array(1) {
[0] =>
bool(true)
}
array(1) {
[0] =>
bool(true)
}
Note 1, it should have returned ‘disabled’ with value true/false.
Note 2, it returned this with >= 10
array(1) {
[0] =>
bool(true)
}
array(1) {
[0] =>
bool(true)
}
array(1) {
[0] =>
bool(true)
}
array(1) {
[0] =>
bool(true)
}
So clearly something was wrong.
I knew that the DB script returns array(true) if you don’t run a select statement, so I looked here
[php]} else {
$ret = [TRUE];
}[/php]
And sure enough, that code did run.
But you ARE running a select statement, so why does it go there…?
The error is in the (sloppy) check if it is a select statement. So change out the DB->query function with this:
[php] public function query($query, $params = [], $fetch_method = ‘OBJ’, $class = ‘’) {
$stmt = $this->pdoConn->prepare($query);
$result = $stmt->execute($params);
if ($result) {
if (strpos(strtolower(trim($query)), 'select') === 0) {
if (strtoupper($fetch_method) === 'CLASS') {
$ret = $stmt->fetchAll(constant('PDO::FETCH_CLASS'), $class);
} else {
$ret = $stmt->fetchAll(constant('PDO::FETCH_' . strtoupper($fetch_method)));
}
} else {
$ret = [TRUE];
}
}
return !empty($ret) ? $ret : null;
}[/php]
I will review the DB code later on and update my tutorial.
Now the page returns this (remember I still var dump out the $result array directly after the query)
[code]array(1) {
[0] =>
class stdClass#4 (1) {
public $disabled =>
string(8) “disabled”
}
}
Fatal error: Cannot use object of type stdClass as array in /srv/www/test/public/jay7981/index.php on line 19
Call Stack:
0.0000 124696 1. {main}() /srv/www/test/public/jay7981/index.php:0[/code]
Pretty straight forward, the DB class is set to return objects as default. So we should either
A: change notation from array to object
[php]$result[0][‘disabled’][/php]–>[php]$result[0]->disabled[/php]
B: change fetch method to array
[php]$result = $db->query(“SELECT
IF(COUNT(class_1) >= 10, ‘disabled’, null) as disabled
FROM hot14
WHERE class_1 = ?”,
array($id));[/php]–>[php]$result = $db->query(“SELECT
IF(COUNT(class_1) >= 10, ‘disabled’, null) as disabled
FROM hot14
WHERE class_1 = ?”,
array($id), ‘ASSOC’);[/php]
I like to work with objects, so I did method A. Now the $result dump shows this:
array(1) {
[0] =>
class stdClass#4 (1) {
public $disabled =>
string(8) "disabled"
}
}
array(1) {
[0] =>
class stdClass#5 (1) {
public $disabled =>
NULL
}
}
array(1) {
[0] =>
class stdClass#3 (1) {
public $disabled =>
NULL
}
}
array(1) {
[0] =>
class stdClass#4 (1) {
public $disabled =>
string(8) "disabled"
}
}
And the disabling works as expected.
Sorry for messing this up for you, I haven’t encountered that bug with the DB script before 