in reply to Username regex, MYSQL random

I wanna check $username to make sure that it only contains letters and numbers and a underscore, anythig else giver error();
error() unless $username =~ m< ^ \w+ \z >x;
Is there in way in SQL to select a row by random, so if it havd 1000 rows it picked row 200.
There isn't a generic way to pick a random row in MySQL, but if you've got a primary key which is an integer and you know the amount of rows you've got then you can just pick a random number and SELECT from that e.g
my $sth = $dbh->prepare( 'SELECT some,rows FROM your_table WHERE id = ?' ); $sth->execute( int rand $dbh->selectrow_array('SELECT MAX(*) FROM your_table') );

HTH

_________
broquaint

update: changed COUNT to MAX (thanks to UnderMine for that one)

Replies are listed 'Best First'.
Re: Re: Username regex, MYSQL random
by UnderMine (Friar) on Nov 23, 2002 at 18:43 UTC
    Sorry to do this to you broquaint ;)

    @rand_row = $dbh->selectrow_array('SELECT some,rows, rand() as orderno FROM your_table ORDER BY orderno LIMIT 1');
    Now that is nicer..

    Hope it helps
    UnderMine

Re: Re: Username regex, MYSQL random
by Anonymous Monk on Nov 23, 2002 at 15:44 UTC
    thanks for your help