note 32226 added to function.mysql-data-seek

From: Date: Tue, 20 May 2003 23:29:25 +0000
Subject: note 32226 added to function.mysql-data-seek
Groups: php.notes 
Request: Send a blank email to php-notes+get-48698@lists.php.net to get a copy of this message
Here is a nice little snippet to return a random row of a result set: <?php // suppose you have a database table of drinks :) // get the data $link = mysql_connect('localhost', 'mysql_user', 'mysql_password') or die('Could not connect: ' . mysql_error()); mysql_select_db('my_database') or die('Could not select database: ' . mysql_error()); $query = 'SELECT temperature, drink FROM drinks'; $result = mysql_query($query) or die('Query failed: ' . mysql_error()); // make sure you have at least one row if (mysql_num_rows($result) > 0) { // fetch one random row mysql_data_seek($result, rand(0, mysql_num_rows($result) - 1)); $row = mysql_fetch_object($result); // output your suggestion print 'you should now have a '.$row->temperature.' glass of '.$row->drink; // advertise for more print 'hit refresh when you are done'; } else { print 'sorry, no drinks in database!'; } ?> ---- Manual Page -- http://www.php.net/manual/en/function.mysql-data-seek.php Edit Note -- http://master.php.net/manage/user-notes.php?action=edit+32226 Delete Note -- http://master.php.net/manage/user-notes.php?action=delete+32226&report=yes Reject Note -- http://master.php.net/manage/user-notes.php?action=reject+32226&report=yes

« previous php.notes (#48698) next »