I'm trying to randomly select one record from a database. At the start of the function I don't know how many records there are in the database. The id fields are not guaranteed to be sequential either.
Could anyone offer some suggestions on the best way to do this?
Code:
// Seed the generator
mt_srand((double)microtime() * 1000000);
// Find out how many rows are in the database
$total = $db->sql_numrows($db->sql_query("SELECT * FROM ".$prefix."_rtc"));
// Is there a better way to do this, because I'd hate to have to select the whole thing when I ultimately only want 1 entry.
// Okay, now I want a random number between 1 and total records in the database
$num = rand(1,$total);
// Okay now to go out and fetch the actual record... What is the where clause??? I definitely don't get that.
$card=$db->sql_query("SELECT * FROM ".$prefix."_rtc" Where ___??___)
list($id, $name, $img_url, $description) = $db->sql_fetchrow($card);
I know this code dosent work, but any suggestions on improving it and a bit of theory on why would really help me a lot.
The first content statement always prints total records in the database. I've never seen SELECT count(*) and I'm having trouble finding it on the net, but I'll keep looking. Anyway, that's working.
The second content always prints out a value from 1 to total records, including the 1 and the total records number. So that's working perfectly!
I'd of cought that quotation being off eventually.
Does anyone know where a good place is to read what the various PHP errors mean online?
I have a PHP Bible, and have been to several places online, but the things I need arent easy to find.
I also need a better understanding of the LIMIT x,x clause because it isent working quite like I thought it would.
Right now it pulls cards one thru (total records -1) correctly, but when the randomizer calls for the last card in the database it dosent work, and it dosent print an error either.
It does pull the first card in the database correctly.
Links to good reference material is fine with me!!
Joined: Aug 27, 2002 Posts: 16987 Location: Kansas
Posted:
Thu Jun 02, 2005 3:11 pm
Why are you doing (total records - 1)? Try (total records). Limit x,y simply says move the data pointer to table record/row x and retrieve y number of rows.
I'm saying that the clause (limit x,y) is working for all random numbers up to one less than total records in the database. It is also working for record number 1... Which only reinforces that I should not be doing total records -1...
Just to be perfectly clear I'll repost it for you here:
Code:
$content = "<table width=\"100%\" border=\"0\">";
// Seed the generator
mt_srand((double)microtime() * 1000000);
// Find out how many rows are in the database
list($total) = $db->sql_fetchrow($db->sql_query("SELECT count(*) FROM ".$prefix."_rtc"));
$content .="<tr><td align=\"center\">".$total."</td></tr>"; // Temporary output used for testing
$num = rand(1,$total);
$content .="<tr><td align=\"center\">".$num."</td></tr>"; // Temporary output used for testing
If the ramdomizer calls by chance for the last record in the database it dosent work. This image is not displayed, and the description is not displayed. No errors.
Any ideas on places I can read up on this stuff please?
View next topic View previous topic
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum