DevChunks

To get a random row from your table, you can use the following trick:

{code type=php}
SELECT * FROM myTable ORDER BY RAND() LIMIT 1
{/code}
This will return a single random row from myTable.

Keep in mind you can also use a WHERE clause to limit the set you are choosing a random record from.

This is a more efficient way of finding a random result than it would be if you returned all the records from the table and looped through them in your code.

Share:

Leave a Reply

Your email address will not be published.

More Posts

How to use PHP Type Declarations

PHP has developed into a more strictly typed language in recent years. Type Declarations help with that and are a fairly new addition to PHP,

Iteration

If you have come to JavaScript from a background in Java or C, then you will like the variation on the for loop. It is

Context Object Manipulation

One of the key concepts of JavaScript is the keyword this, which signifies the JavaScript context object. If you write an inner function, you can access

Polymorphic Functions

It is possible to define multiple functions with the same name in JavaScript, and the last one of those that you define will be the