How would i do this?
At the moment i'm selecting a random number from 1 to the total number of rows. However, if i deleted a row in the table, it could still come up with a number that doesn't exist in the primary key column. Therefore, i need a way to either:
- detect that the primary key exists and if not, pick a random number again.
- or, create an array of all the primary keys and select a random number from them.
Any help, much appreciated!!
here is my existing code if it helps:
Code: Select all
/* Get total number of rows */
$count1 = mysql_query("SELECT * FROM slides");
$count = mysql_num_rows($count1);
/* Generate random number and get info based on pkey and random number */
$randSlide = mt_rand(1,$count);
$result2 = mysql_query("SELECT snapimg,title,pshortdesc FROM slides WHERE pkey = '$randSlide'");
$rowTotal = mysql_num_rows($result2);