Skip to content

Instantly share code, notes, and snippets.

@Relequestual
Last active December 17, 2015 08:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Relequestual/5583353 to your computer and use it in GitHub Desktop.
Save Relequestual/5583353 to your computer and use it in GitHub Desktop.
Get total count for use with pagination for MySQL
//if your query is raw sql you created, each mysql key word must be on a new line for this to work
$last_query = $this->db->last_query();
$pattern = '/(.*)Limit (.*)/i';
$new_query = preg_replace ($pattern, '', $last_query);
$pattern = '/(.*)Select *(.*)/i';
$new_query = preg_replace ($pattern, 'SELECT \'\'', $new_query);
$new_query = 'Select Count(*) AS res From (' . $new_query . ') v__dynamic;';
$query = $this->db->query($new_query);
$result = $query->row();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment