note 34495 added to function.rand

From: Date: Tue, 29 Jul 2003 10:38:34 +0000
Subject: note 34495 added to function.rand
Groups: php.notes 
Request: Send a blank email to php-notes+get-52891@lists.php.net to get a copy of this message
On the subject below where the user wants to select a random tuple from a relation in a MySQL database. To select a random entry, order on RAND(). This is supported upwards from MySQL version 3.23. As an example on the user's problem below: /* query is split over multiple lines, to enhance readability */ $q = "SELECT imagepath, website \\n". "FROM resources \\n". "WHERE website != '' AND imagepath != '' \\n". "ORDER BY rand() \\n". "LIMIT 1"; $result = mysql_query($q) or die ("Could not execute query: $q".mysql_error()); $obj = mysql_fetch_object($result); /* use the object */ ---- Manual Page -- http://www.php.net/manual/en/function.rand.php Edit Note -- http://master.php.net/manage/user-notes.php?action=edit+34495 Delete Note -- http://master.php.net/manage/user-notes.php?action=delete+34495&report=yes Reject Note -- http://master.php.net/manage/user-notes.php?action=reject+34495&report=yes

« previous php.notes (#52891) next »