Sweeper, I am working on something to pull a random row from a mysql database. In my case, I am pulling faqs.
//get a random number from the total number of faqs in the db.
$db = mysql_connect("", "", "");
mysql_select_db("", $db);
$result = mysql_query("SELECT * FROM FAQS", $db);
$num = mysql_num_rows($result);
//gives me the total number of rows in the table
//random number between 1 and the total number of rows in the database
//if there are 20 faq's, will pull number between 1 and 20
$random_number = rand(1,$num);
$faq_result = mysql_query("SELECT * FROM FAQS WHERE id = $random_number", $db);
//pull a row from the db based on the random number
$row = mysql_fetch_array($faq_result);
There may be a better way to do this and I am still working on it. The problem I have is what to do when a faq is deleted from the db, the numbering which may ber 1-20, would have a missing number if one is deleted.
Hope this gets you started
sweeper wrote:
'lo...
i've a little question, as the subject indicated, i'm looking for a possibility to fecht a random row from a mysql table. is there any way to do this easily? i know there's a function mysql_fetch_row() but after reading the manual, I don't think this is what I need, as you can't really select any row from the table, but only after querying the database, and that's not what I want.
is there anybody who has a solution at hand? thanks in advance
sweeper