note 101524 added to function.rand

From: Date: Wed, 22 Dec 2010 07:13:56 +0000
Subject: note 101524 added to function.rand
Groups: php.notes 
Request: Send a blank email to php-notes+get-175137@lists.php.net to get a copy of this message
I had to come up with a quick way to get a random row from a table, and came up with the following: <?php $query = "SELECT id FROM [TABLE]"; $result = mysql_query($query, $link); $num = mysql_num_rows($result); if ($num > 0) { // Array the ID's... $id_array = array(); while ($row = mysql_fetch_assoc($result)) { $id_array []=$row["id"]; } $query = "SELECT * FROM [TABLE] WHERE id=".$id_array[rand(0, (count($id_array)-1))]; $result = mysql_query($query, $link); $row = mysql_fetch_assoc($result); // ... } ?> ---- Server IP: 69.147.83.197 Probable Submitter: 124.168.160.31 ---- Manual Page -- http://www.php.net/manual/en/function.rand.php Edit -- https://master.php.net/note/edit/101524 Del: integrated -- https://master.php.net/note/delete/101524/integrated Del: useless -- https://master.php.net/note/delete/101524/useless Del: bad code -- https://master.php.net/note/delete/101524/bad+code Del: spam -- https://master.php.net/note/delete/101524/spam Del: non-english -- https://master.php.net/note/delete/101524/non-english Del: in docs -- https://master.php.net/note/delete/101524/in+docs Del: other reasons-- https://master.php.net/note/delete/101524 Reject -- https://master.php.net/note/reject/101524 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#175137) next »