Vote count:
0
I have been trying to output data from a db table but I can't seem to do it, it doens't show up no matter what I do. I tried using foreach but no dice. This code was taken from a phpacademy tutorial and I am trying to modify it by adding a bit of functionality like being able to gather all data from the database using pdo
$bgItems = DB::getInstance()->getAll('background_image');
echo '<pre>', var_dump($bgItems), '</pre>';
foreach($bgItems as $bgItem) {
echo $bgItem;
}
This is what the DB class looks like with the method i'm trying to implement: public function query($sql, $params = array()) {
$this->_error = false;
if($this->_query = $this->_pdo->prepare($sql)) {
$x = 1;
if(count($params)) {
foreach($params as $param) {
$this->_query->bindValue($x, $param);
$x++;
}
}
if($this->_query->execute()) {
$this->_results = $this->_query->fetchAll(PDO::FETCH_OBJ);
$this->_count = $this->_query->rowCount();
} else {
$this->_error = true;
}
}
return $this;
}
public function action($action, $table, $where = array()) {
if(count($where) === 3) {
$operators = array('=', '>', '<', '>=', '<=');
$field = $where[0];
$operator = $where[1];
$value = $where[2];
if(in_array($operator, $operators)) {
$sql = "{$action} FROM {$table} WHERE {$field} {$operator} ?";
if(!$this->query($sql, array($value))->error()) {
return $this;
}
}
}
else {
$sql = "{$action} FROM {$table}";
if(!$this->query($sql)->error()) {
return $this;
}
}
return false;
}
public function getAll($table) {
return $this->action('SELECT *', $table);
}
And this is the output I get when trying to view the data retrieved using var_dump, which obviously looks like a complicated array.
object(DB)#3 (5) {
["_pdo":"DB":private]=>
object(PDO)#4 (0) {
}
["_query":"DB":private]=>
object(PDOStatement)#8 (1) {
["queryString"]=>
string(30) "SELECT * FROM background_image"
}
["_error":"DB":private]=>
bool(false)
["_results":"DB":private]=>
array(3) {
[0]=>
object(stdClass)#7 (2) {
["id"]=>
string(1) "1"
["name"]=>
string(28) "4ee269991861331957002e21.jpg"
}
[1]=>
object(stdClass)#9 (2) {
["id"]=>
string(1) "2"
["name"]=>
string(36) "22769-interesting-cat-meme-rv31.jpeg"
}
[2]=>
object(stdClass)#10 (2) {
["id"]=>
string(1) "3"
["name"]=>
string(50) "10645322_300758350130075_5393354656605964412_n.jpg"
}
}
["_count":"DB":private]=>
int(3)
}
asked 13 secs ago
PDO, output all data from a table
Aucun commentaire:
Enregistrer un commentaire