我正在擴展PDO類以添加操作準備語句的查詢字符串的函數。例如,使查詢可搜索或添加分頁。使用PDO修改預準備語句的查詢字符串
例如:
$documents_query = $DB->prepare("SELECT id, title, file_name, datetime_added
FROM documents
ORDER BY datetime_added DESC");
$documents_query->paginate($page_number, RESULTS_PER_PAGE);
的問題是如何修改查詢字符串(這是隻讀),並保存它,所以它可以得到以後執行?
這是我的擴展PDOStatement對象類看起來像一個例子:
class CustomStatement extends PDOStatement
{
public function paginate($current_page, $max_results)
{
// Add SQL_CALC_FOUND_ROWS so we can count the total amount of results
$select_index = stripos($this->queryString, 'SELECT');
$statement = substr_replace($this->queryString, 'SELECT SQL_CALC_FOUND_ROWS', $select_index, 6);
// Add LIMIT to the end of the query
$start_limit = ($current_page - 1) * $max_results;
$statement = $statement . ' LIMIT ' . $start_limit . ', ' . $max_results;
// What to do here?
return $this->prepare($statement);
}
}
'paginate'你的新'準備'? – hakre
這不是打算。如果要執行不同的查詢,則需要創建一個新的準備好的語句句柄。 – mario
我想也許你不懂準備好的陳述。準備好的語句被髮送到保存它的服務器,等待數據插入並執行。一旦它準備好了,mySQL服務器就擁有了它 - 你不能再對它做任何事情。 – Erik