Hello I have the following code which returns the amount of rows in a table and the picks a random number between 1 and the amount of rows. I want to be able to get the row in the MySQL table that corresponds to the random number. So if the number is two, I want to get the second row down.
Here is my code so far:
$query = mysql_query("SELECT count(*) as total from videos");
$result = mysql_fetch_array($query);
$TotalCount = $result['total'];
$RandRow = mt_rand(1,$TotalCount);
You want one random row out of your table, right? Then you could use ORDER BY RAND() in your query and LIMIT 1. It sorts - surprise - randomly and only returns one dataset.
$query = mysql_query("SELECT * FROM videos ORDER BY RAND() LIMIT 1");
So you just have to do one query.