Random record from mysql database with CodeIgniter Random record from mysql database with CodeIgniter codeigniter codeigniter

Random record from mysql database with CodeIgniter


Codeigniter provides the ability to order your results by 'RANDOM' when you run a query. For instance

function get_random_page(){    $this->db->order_by('id', 'RANDOM');    or    $this->db->order_by('rand()');    $this->db->limit(1);    $query = $this->db->get('pages');    return $query->result_array();}

I've used this before and found it to work fine. Hope that helps


I don't know about codeigniter, but getting a random dataset is

SELECT * FROM table ORDER BY RAND() LIMIT 1

The relevant part is "ORDER BY RAND()", obviously.


Do you know how many records there are in the table? You could do something like this:

$count=mysql_exec('select count(*)-1 from some_table');$count=rand(1,$count);

then:

select * fromsome_Tablelimit $count,1