Results 1 to 12 of 12

Thread: [RESOLVED] PHP/MySQL Random Record selection[Help Needed]

  1. #1

    Thread Starter
    Fanatic Member modpluz's Avatar
    Join Date
    Sep 2005
    Location
    Lag, NG
    Posts
    633

    Resolved [RESOLVED] PHP/MySQL Random Record selection[Help Needed]

    pls how do you select random datas from a MySQL database(really random).

    say i have numerous records and i want to select 4(or more) random records each time the page is refreshed or re-visited.

    i have read some tutorial/s but it doesn't help my situation and i have also read about the MySQL RAND() function(but i couldn't use it).

    thank you.
    If you want the rabbit to hop, move the carrot - Paul Kellerman(Prison Break)

    onError GoTo http://vbforums.com



    My Bits:
    VB6: Change Column Name in MS ACCESS

  2. #2
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: PHP/MySQL Random Record selection[Help Needed]

    Why can you not use the mysql RAND() function? Do you have some kind of moral objection to it?
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  3. #3
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: PHP/MySQL Random Record selection[Help Needed]

    You use it in the order by statement:
    Code:
    SELECT * FROM table ORDER BY RAND();
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  4. #4

    Thread Starter
    Fanatic Member modpluz's Avatar
    Join Date
    Sep 2005
    Location
    Lag, NG
    Posts
    633

    Re: PHP/MySQL Random Record selection[Help Needed]

    oh!, from what i read, i thought you can only use RAND() for just one record as in RAND(0, 1).
    If you want the rabbit to hop, move the carrot - Paul Kellerman(Prison Break)

    onError GoTo http://vbforums.com



    My Bits:
    VB6: Change Column Name in MS ACCESS

  5. #5
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: PHP/MySQL Random Record selection[Help Needed]

    Well you can randomize the order of one record if you like. It will be a little it of pointless process
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  6. #6
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: PHP/MySQL Random Record selection[Help Needed]

    Do not use RAND in an ORDER BY clause. This is an O(n) operation which is very slow for large data sets.
    Instead, to pick one record at random, just generate a random number using PHP and use that in a LIMIT clause. You can use a SELECT COUNT() query to get the number of records in the table.

    PHP Code:
    $count $db->query_read_scalar('SELECT COUNT(m.keyid) FROM mytable m');
    $random_record $db->query_read('SELECT m.* FROM mytable m LIMIT '.rand(0$count).', 1'); 
    This is O(n) too, but much faster.
    Last edited by penagate; Mar 8th, 2008 at 02:09 PM.

  7. #7
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: PHP/MySQL Random Record selection[Help Needed]


  8. #8
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: PHP/MySQL Random Record selection[Help Needed]

    This is only usually an issue when you have a very large dataset. I should have mentioned it because I ran into problems with it before

    I used something like this which is still within SQL query itself: http://forums.mysql.com/read.php?24,...316#msg-192316
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  9. #9
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: PHP/MySQL Random Record selection[Help Needed]

    Yes, that's the way used in the "better solution" above.

  10. #10
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: PHP/MySQL Random Record selection[Help Needed]

    The one I found is better because it is on the MySQL forum
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  11. #11
    New Member
    Join Date
    Jun 2009
    Posts
    1

    Re: PHP/MySQL Random Record selection[Help Needed]

    Quote Originally Posted by visualAd View Post
    Why can you not use the mysql RAND() function? Do you have some kind of moral objection to it?
    Correct me if I am wrong, but this doesn't actually return a random row. All it does is randomly select a column by which to order your results. This may give the appearance of random row, especially if your table has loads of columns. But what if it only has one column? You'll get the same order every time.

  12. #12
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: PHP/MySQL Random Record selection[Help Needed]

    Quote Originally Posted by loquela View Post
    Correct me if I am wrong, but this doesn't actually return a random row. All it does is randomly select a column by which to order your results. This may give the appearance of random row, especially if your table has loads of columns. But what if it only has one column? You'll get the same order every time.
    I am not quite sure what you are getting at.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width