Randow choose a username in mysql

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
aikoman
Forum Newbie
Posts: 2
Joined: Wed Oct 19, 2011 10:17 pm

Randow choose a username in mysql

Post by aikoman »

HI, i'm new here< i was looking for a forum about php and the first i found is you guys.

I got a question. I want to make a database where people can register with full name and email. and I wanted to know how to make a script that i can use like a raffle so that people that register can win prices. so I need a php script that when i click (choose a winner) it randomly choose a name from my database so they can win a price.

can anyone of you guys help me with this??

I'm a beginner in php.

thanks
AIKOMAN
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Randow choose a username in mysql

Post by Benjamin »

Code: Select all

<?php
# @see http://www.greggdev.com/web/articles.php?id=6
# @foundOn google.com/search?q=choosing+random+mysql+row&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a
  //CODE FROM http://WWW.GREGGDEV.COM

  function random_row($table, $column) {
      $max_sql = "SELECT max(" . $column . ") 
                  AS max_id
                  FROM " . $table;
      $max_row = mysql_fetch_array(mysql_query($max_sql));
      $random_number = mt_rand(1, $max_row['max_id']);
      $random_sql = "SELECT * FROM " . $table . "
                     WHERE " . $column . " >= " . $random_number . " 
                     ORDER BY " . $column . " ASC
                     LIMIT 1";
      $random_row = mysql_fetch_row(mysql_query($random_sql));
      if (!is_array($random_row)) {
          $random_sql = "SELECT * FROM " . $table . "
                         WHERE " . $column . " < " . $random_number . " 
                         ORDER BY " . $column . " DESC
                         LIMIT 1";
          $random_row = mysql_fetch_row(mysql_query($random_sql));
      }
      return $random_row;
  }
  
  //USAGE
  echo '<pre>';
  print_r(random_row('YOUR_TABLE', 'YOUR_COLUMN'));
  echo '</pre>';
?>
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: Randow choose a username in mysql

Post by twinedev »

Code: Select all

SELECT `name`,`email` FROM `users` WHERE `status`='active' ORDER BY RAND() LIMIT 1
The key thing is the your are ordering by a random number, and limiting to just the first result.

Add other things to WHERE as needed

-Greg
aikoman
Forum Newbie
Posts: 2
Joined: Wed Oct 19, 2011 10:17 pm

Re: Randow choose a username in mysql

Post by aikoman »

Thanks you guys very much.!!
User avatar
egg82
Forum Contributor
Posts: 156
Joined: Sat Oct 01, 2011 9:29 pm
Location: Colorado, USA

Re: Randow choose a username in mysql

Post by egg82 »

man, you guys are fast O.o

Always happy to help!

Update: Just looked at the dates :lol:
Post Reply