簡體   English   中英

PDO,從表中輸出所有數據

[英]PDO, output all data from a table

我一直在嘗試從數據庫表中輸出數據,但是我似乎無法做到這一點,無論我做什么,它都不會出現。 我嘗試使用foreach但沒有骰子。 該代碼取自phpacademy教程,我試圖通過添加一些功能來對其進行修改,例如能夠使用pdo從數據庫收集所有數據。

$bgItems = DB::getInstance()->getAll('background_image');

echo '<pre>', var_dump($bgItems), '</pre>';

foreach($bgItems as $bgItem) {
    echo $bgItem;
}

這是我嘗試實現的方法的DB類的外觀:

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);
    }

這是我嘗試查看使用var_dump檢索的數據時得到的輸出,這顯然看起來像一個復雜的數組。

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)

}

更新

直接返回從DB類的protected _results變量獲取的結果時的結果

    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"
  }
}

您需要返回結果而不是類本身,可以使用一個類,但是需要實現迭代器。

public function getAll($table) {
        if( $this->action('SELECT *', $table) ) return $this->_results;
        return false;

}

我只是將對象轉換為數組,以便使用此對象進行操作

class Convert {
    public function objectToArray($d) 
    {
        if (is_object($d)) {
            // Gets the properties of the given object
            // with get_object_vars function
            $d = get_object_vars($d);
        }

        if (is_array($d)) {
            /*
            * Return array converted to object
            * Using __FUNCTION__ (Magic constant)
            * for recursive call
            */
            return array_map([__CLASS__, __METHOD__], $d);
        } else {
            // Return array
            return $d;
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM