Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

How to read a random database or multiple

I need to read a random record from the database, how to write

$model = new articles(); $model::findFirst(array('order'="??????????") );



2.6k
Accepted
answer

One way,especially if the table is fairly small, you can order by RAND(). But the way you initiated the modal the second line would be: $model->findFirst(array('order' => 'RAND()'));

Another method for larger data would be to find max id, if you have auto increase integer id, then select a random number using PHP function.