note 34495 deleted from function.rand by didou

From: Date: Tue, 29 Jul 2003 10:50:24 +0000
Subject: note 34495 deleted from function.rand by didou
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-52893@lists.php.net to get a copy of this message
Note Submitter: Edwin de Jong (mauddib at gmx dot net) ---- 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 */

« previous php.notes (#52893) next »