Jump to content


Photo

random articles


  • Please log in to reply
6 replies to this topic

#1 feri_soft

feri_soft
  • Members
  • PipPipPip
  • Advanced Member
  • 147 posts

Posted 29 May 2006 - 06:51 AM

Can someone explain me how it's done.For example u have a DB(mysql).And how to select and list lets say 5 random articles from it.Can you tell me please?? [img src=\"style_emoticons/[#EMO_DIR#]/smile.gif\" style=\"vertical-align:middle\" emoid=\":smile:\" border=\"0\" alt=\"smile.gif\" /]

#2 poirot

poirot
  • Members
  • PipPipPip
  • Advanced Member
  • 646 posts
  • LocationAustin, TX

Posted 29 May 2006 - 07:12 AM

AFAIK, basically there is no built-in function in PHP or MySQL that allows you to do that. You'll have to use a workaround. A Google Search may be helpful.
~ D Kuang

#3 feri_soft

feri_soft
  • Members
  • PipPipPip
  • Advanced Member
  • 147 posts

Posted 29 May 2006 - 04:52 PM

there is something like

select * from x order by rand() limit 5

but is that enought??



#4 poirot

poirot
  • Members
  • PipPipPip
  • Advanced Member
  • 646 posts
  • LocationAustin, TX

Posted 29 May 2006 - 04:53 PM

Probably it works and it's effective if you have a few rows, but I think this could cause problems (use a lot of resources) if there are many rows.
~ D Kuang

#5 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 29 May 2006 - 09:01 PM

On a table containing 10,000+ records I got this level of performance, selecting and printing 5 random records

$t1 = microtime(true);
$res = mysql_query("SELECT COUNT(*) FROM sales") or die(mysql_error());
echo '<pre>';
echo mysql_result($res, 0), ' records searched<br/>';

$res = mysql_query("SELECT * FROM sales ORDER BY RAND() LIMIT 5") or die(mysql_error());
while (list(,$code,$value) = mysql_fetch_row($res)) {
    printf ('%3s %8.2f<br/>', $code, $value);
}
$t2 = microtime(true);
$t = $t2-$t1;
printf ('Time taken %0.3f seconds', $t);
echo '</pre>';

Output -->

10917 records searched
005    16.00
005     8.00
750    66.00
724     6.40
004   175.37
Time taken 0.018 seconds

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#6 poirot

poirot
  • Members
  • PipPipPip
  • Advanced Member
  • 646 posts
  • LocationAustin, TX

Posted 06 June 2006 - 03:34 AM

Barand, I found the article:

[a href=\"http://www.greggdev.com/web/articles.php?id=6\" target=\"_blank\"]http://www.greggdev.com/web/articles.php?id=6[/a]

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]$random_row = mysql_fetch_row(mysql_query("select * from YOUR_TABLE order by rand() limit 1"));

$random_row will be an array containing the data extracted from the random row. However, when the table is large (over about 10,000 rows) this method of selecting a random row becomes increasingly slow with the size of the table and can create a great load on the server. I tested this on a table I was working that contained 2,394,968 rows. It took 717 seconds (12 minutes!) to return a random row.[/quote]
~ D Kuang

#7 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 06 June 2006 - 06:26 AM

I am not disagreeing, but their threshold of 10,000 is a little pessimistic.
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users