note 89343 added to function.rand
| From: | janoserki | Date: | Wed, 04 Mar 2009 08:16:57 +0000 |
| Subject: | note 89343 added to function.rand | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-151435@lists.php.net to get a copy of this message | ||
Easy way for mysql: random row
the original form is: "... order by rand()"
but this is not the best way, because it's very slow by a big database (it can take more
minutes to complete the request!)
My suggestion:
<?php
// estimate the number of rows in a table
$lekerdezes = mysql_query("select * from table");
$max = mysql_num_rows($lekerdezes);
// pick one
$rand = rand(1,$max);
$lekerdezes2 = mysql_query("select * from table limit $rand, 1");
// that's all folks :)
?>
----
Server IP: 62.112.195.86
Probable Submitter: 84.2.147.147
----
Manual Page -- http://www.php.net/manual/en/function.rand.php
Edit -- https://master.php.net/note/edit/89343
Del: integrated -- https://master.php.net/note/delete/89343/integrated
Del: useless -- https://master.php.net/note/delete/89343/useless
Del: bad code -- https://master.php.net/note/delete/89343/bad+code
Del: spam -- https://master.php.net/note/delete/89343/spam
Del: non-english -- https://master.php.net/note/delete/89343/non-english
Del: in docs -- https://master.php.net/note/delete/89343/in+docs
Del: other reasons-- https://master.php.net/note/delete/89343
Reject -- https://master.php.net/note/reject/89343
Search -- https://master.php.net/manage/user-notes.php