Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    zc1
    zc1 is offline
    New Coder
    Join Date
    Jun 2007
    Posts
    29
    Thanks
    8
    Thanked 0 Times in 0 Posts

    [solved]Random MySQL Results

    Hi,

    I am trying to get a random result from a database table to display random pictures for users if they have more then one picture, at the moment I have it so it pulls the same picture from the database, code below and I called it by putting '.$picsno.'
    Code:
    $picsno = $db->getOne('select picno from ! where userid = ?', array(USER_SNAP_TABLE, $userid) );
    I have tried
    ORDER BY rand() LIMIT 1 and that didn't work

    Can anyone help ?

    Regards,
    Garry
    Last edited by zc1; 06-05-2007 at 09:37 AM.

  • #2
    Regular Coder westmatrix99's Avatar
    Join Date
    Dec 2006
    Location
    South Africa
    Posts
    307
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Have you tried:
    PHP Code:
    $picsno "SELECT * FROM picno where userid = $userid  ORDER BY rand() Limit 1";
    $result mysql_query($picsno); 
    You need to be able to view the images from the picno table by a specific user?
    The limit only limits the data that you are outputting, you could show 1 record from picno per user that matches the query?

    Also just out of interest:
    PHP Code:
     $query "SELECT * FROM picno";
    $result $mysql->SelectLimit($query,1) or die($mysql->ErrorMsg());// you could change this ($query,1) to($query,3) instead 
    Last edited by westmatrix99; 06-04-2007 at 02:05 PM.
    Thanks for you support!

  • #3
    zc1
    zc1 is offline
    New Coder
    Join Date
    Jun 2007
    Posts
    29
    Thanks
    8
    Thanked 0 Times in 0 Posts
    Hi,

    Thanks for your reply

    No the result need to be from osdate_usersnaps (if using an array USER_SNAP_TABLE) and the field I need to be pulling data from in picno

    I need to pull 1 picno per userid to display the picture, but need it to be random

    I tried your first lot of code but it gave me a white page so I tried
    Code:
    $picsno = $db->getOne('select picno from ! where userid = ? ORDER BY rand() LIMIT 1', array(USER_SNAP_TABLE, $userid) );
    $result = mysql_query($picsno);
    This gave me a different picture, but it still the same all the time

    Regards,
    Garry

  • #4
    New to the CF scene
    Join Date
    May 2007
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I am no good with arrays, maybe you change the array part to also have rand() in it?

    Also do you actually have a tablename as "!" and a userid as "?", if so then there is your problem, I think?

  • #5
    zc1
    zc1 is offline
    New Coder
    Join Date
    Jun 2007
    Posts
    29
    Thanks
    8
    Thanked 0 Times in 0 Posts
    Hi,

    I am not much good with arrays either, just the script I am using (osDate) uses them

    When using the array ! = USER_SNAP_TABLE (for the array bit)
    and ? = $userid (for the array bit)

    Regards,
    Garry

  • #6
    Regular Coder westmatrix99's Avatar
    Join Date
    Dec 2006
    Location
    South Africa
    Posts
    307
    Thanks
    12
    Thanked 0 Times in 0 Posts
    PHP Code:
    $picsno $db->getOne('select picno from USER_SNAP_TABLE where userid = $userid ORDER BY rand() LIMIT 1';
    $result mysql_query($picsno); 
    Just out of interest, why the array?
    PHP Code:
    array(USER_SNAP_TABLE$userid
    it looks like you are doing (LIMIT 1,by array), is that right?
    Also what is the number the array is returning?
    Thanks for you support!

  • #7
    zc1
    zc1 is offline
    New Coder
    Join Date
    Jun 2007
    Posts
    29
    Thanks
    8
    Thanked 0 Times in 0 Posts
    Hi,

    I was using the array as that is what the script was using I am running this in, I hae now got rid of the array and my code is
    Code:
    $picsno = $db->getOne('select picno from osdate_usersnaps where userid = '.$userid.' ORDER BY rand() LIMIT 1');
    $result = mysql_query($picsno);
    As for the number it getting:
    Without ORDER BY rand() LIMIT 1 it gets: 1
    With ORDER BY rand() LIMIT 1 it gets: 2 where the user has more then one image, but it does not give a random picture each time they visit it

    Regards,
    Garry

  • #8
    Regular Coder westmatrix99's Avatar
    Join Date
    Dec 2006
    Location
    South Africa
    Posts
    307
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Found this tut, give it a go:
    http://www.pixel-designz.net/tutoria...ure_Generator/

    Also there is a random image script here:
    http://www.tecnorama.org/document.php?id_doc=57
    and here:
    http://www.tecnorama.org/document.php?id_doc=58

    Hope that helps you?
    Cheers
    Thanks for you support!

  • #9
    zc1
    zc1 is offline
    New Coder
    Join Date
    Jun 2007
    Posts
    29
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Cool

    Hi,

    Think I may have got it seems to work ok, can someone confirm
    PHP Code:
    $piccnt $db->getAll('select picno from ! where userid = ? and ( album_id is null or album_id = ?)',array(USER_SNAP_TABLE$userid,'0' ) );
    $picsno $piccnt[array_rand($piccnt)]['picno']; 
    Which I think is the same, but without the array as below
    PHP Code:
    $piccnt $db->getAll('select picno from osdate_usersnaps where userid = '.$userid.' and ( album_id is null or album_id = '0') ');
    $picsno $piccnt[array_rand($piccnt)]['picno']; 
    Regards,
    Garry


  •  

    Posting Permissions

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