I'm extending the PDO class to add functions that manipulate the querystring of a prepared statement. For example make a query searchable or add pagination.
For example:
$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 );
The question is how to modify the querystring (which is readonly) and save it, so it can get executed later on?
This is an example of how my extended PDOStatement class looks like:
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 );
}
}