好吧,我從來沒有使用動態函數,不知道爲什麼,我從來沒有喜歡使用爆炸(),內爆()等PHP使用while循環來獲取項目,使用動態選擇功能?
但我試過了,出了點問題。
public function fetch($table, array $criteria = null)
{
// The query base
$query = "SELECT * FROM $table";
// Start checking
if ($criteria) {
$query .= ' WHERE ' . implode(' AND ', array_map(function($column) {
return "$column = ?";
}, array_keys($criteria)));
}
$check = $this->pdo->prepare($query) or die('An error has occurred with the following message:' . $query);
$check->execute(array_values($criteria));
$fetch = $check->fetch(PDO::FETCH_ASSOC);
return $fetch;
}
這是我的查詢。
基本上我會返回包含獲取方法的變量$fetch
。
,然後某處,在這裏我想用while循環來獲取數據,我將使用:
$r = new Database();
while ($row = $r->fetch("argonite_servers", array("server_map" => "Wilderness")))
{
echo $row['server_map'];
}
現在,我沒有得到任何錯誤,但是瀏覽器加載和加載永遠,並且最終會由於內存不足而卡住。
這是因爲循環運行和運行沒有停止。 它爲什麼這樣做?我怎樣才能使這個動態查詢工作?
編輯:
$r = new Database();
$q = $r->fetch("argonite_servers", array("server_map" => "Wilderness"));
while ($row = $q->fetch(PDO::FETCH_ASSOC))
{
echo $row['server_map'];
}
'的foreach($ R->取(....)爲$項目){}' –