Select random row using PDO What do you think of this way to generate a random number from the total rows in a table? I would like to create a page that random.php precisely generates a random ID and shows it.

require 'includes/config.php';

$pdo-&gt;query('SELECT id FROM xxxx');
$pdo-&gt;resultset();
$total = $pdo-&gt;rowCount();

//echo random id
$id = rand(1, $total);

$pdo-&gt;query('SELECT * FROM xxxxx WHERE id = :id AND status = :status');
$pdo-&gt;bind(':id', $id);
$pdo-&gt;bind(':status', 1);
$rand = $pdo-&gt;single();

echo $rand['id'];
This query will return a random id from the DB with one simple SQL statement instead of using PHPs built in rand function. 

MySQL:

$pdo-&gt;query('SELECT id FROM xxxxx ORDER BY RAND() LIMIT 1');


MS SQL:

$pdo-&gt;query('SELECT id FROM xxxxxx order by NEWID()');


I'm not entirely sure what database you're using, but this would eliminate a few lines of code. 

Also, I'm not sure if you're using a database class to wrap around PHPs PDO class but I generally build an array and store my bound parameters in it then execute it. Looks a little neater.