note 16162 added to function.rand

From: Date: Thu, 18 Oct 2001 12:37:15 +0000
Subject: note 16162 added to function.rand
Groups: php.notes 
Request: Send a blank email to php-notes+get-19088@lists.php.net to get a copy of this message
# Simple and faster (I think) - but not 100% correct - # code to get a random row from a table with mysql < 3.23 # (in 3.23 you can use SELECT * FROM $table ORDER BY RAND() ) srand((double)microtime()*1000000 ); $arrRes = mysql_query("SELECT intId FROM tblBild ORDER BY intId DESC LIMIT 1"); $intMax = mysql_result($arrRes,0); $intMax = rand(0,$intMax); $query = "SELECT intFilmNr,strFileName FROM tblBild WHERE intId >= '$intMax' LIMIT 1"; list($intNr,$strName) = mysql_fetch_array( mysql_query($query) ); $src = "film/preview/$intNr/$strName"; $strImage = "<IMG SRC='$src' align=right>"; # NOTE! If you have 1,2,3,4,200 as intId, # The row 200 will more often be the chosen one. -- http://www.php.net/manual/en/function.rand.php http://master.php.net/manage/user-notes.php?action=edit+16162 http://master.php.net/manage/user-notes.php?action=delete+16162 http://master.php.net/manage/user-notes.php?action=reject+16162

« previous php.notes (#19088) next »