Displaying Random Images from a mySQL Database

Josh-Josh- Royal Oak, MI
edited November 2003 in Internet & Media
I have been wondering on how to display a random image, via information from a mySQL database. I understand that to pull random information from a text file, ect, you would use something similar to:
[php]
<?
srand((double)microtime()*1000000);
$arry_txt=preg_split("/--NEXT--/",join('',file("titles.txt")));
echo$arry_txt[rand(0,sizeof($arry_txt)-1)];
?>
[/php]
Is there a way to modify into selecting from a mySQL database?

Possibly just change titles.txt to a variable ($images), and then defining the variable to the database, some how?

Or, modifying the mySQL query..to make it more simple..I haven't worked with mySQL databases, I assume I would change "desc" to something different?
[php]
$result = mysql_query("select * from gallery order by id desc limit 25");
[/php]

Comments

  • Park_7677Park_7677 Missouri Member
    edited November 2003
    The MySQL query you posted just selects the last 25 images in the database. Not really random.

    1) If you want just 1 image, just simply get a random number (from the LOW and HIGH of "ID") and then query it.

    2) If you want more than one, you can repeat the above over and over until you get the desired amount.

    3) Or.. if the amount of data in the database is somewhat small, you can call it all. Then get random numbers from the LOW and HIGH of the returned array. Call them ($returned_array_name[$rnd_num]) and discard the "left overs" of the array. This way is sloppy and resource wasteful if the database is of decent size.



    Pick an option you want to know more about and I'll post back, and then eventually help you write the code. :)
  • lsevaldlsevald Norway Icrontian
    edited November 2003
    I don't have PHP/MySQL up and running right now, but I think this should work:

    SELECT * FROM gallery ORDER BY RAND()
  • Park_7677Park_7677 Missouri Member
    edited November 2003
    Isevald is correct... there's that too :p I need to wake up :doh:


    His query returns all the images, just in random order. You can limit the number returned by adding LIMIT [NUMBER] to the end.

    [PHP]SELECT * FROM gallery ORDER BY RAND() LIMIT 1[/PHP]

    Returns 1 image randomly.

    GJ Isevald :thumbsup:
  • Josh-Josh- Royal Oak, MI
    edited November 2003
    Yes, thats exactly what I wanted to do Isevald..I just haven't played with mySQL databases in a while, and I couldn't remember. Thanks for the help guys...I've got to get back into mySQL...gets a to be a pain when I forget things..or mess up some syntax.

    Well, thanks.
    -J
    Thank you also Park..
  • KhaosKhaos New Hampshire
    edited November 2003
    Using low and high of ID is not necessarily fool proof because a record can be deleted and then there will be a missing ID until a new record is added to the database. To get around this, check to see if the query returns a record for that ID, and if not, select +/- 1 of the ID depending on whether the random number is < high ID. Maybe I'm wrong about the IDs not changing when records are deleted? I have been drinking tonight, and I haven't coded the PHP/MySQL combo in about 2 years.

    As for the MySQL RAND() dealie, I'm not sure what is faster, using the MySQL rand or getting the ID range and then using PHP to select a random record. The SQL query is probably faster, but you'd have to bench it to know for sure. Dunno what scale site you're working on, but if you end up working with high traffic, you WILL obsesses about optimization. ;)
  • Park_7677Park_7677 Missouri Member
    edited November 2003
    You're right about the LOW & HIGHs of the ID (unless when deleted they were reformatted). It could be done, but would take more coding than a MySQL query doing the same exact thing. I wasn't wide awake when I first posted...:eek3:

    We'll use logic to see which way of doing it is faster: RAND() or PHP handled.

    If it's a small traffic/database site then you wont notice the .00000001 second difference the "other way" could have saved. Who cares?

    And if it were a large traffic/database site, then calling the whole database table into PHP would be a waste of resources (calling a 20 KB database for ~1KB of data for 100s of clients = bad ;[).



    MySQL RAND() = WINNAR :rockon:
  • Josh-Josh- Royal Oak, MI
    edited November 2003
    I'm not sure how much traffic the photo gallery will generate, and I think that MySQL RAND() will work just find. Thanks to all who helped out this thread...
    -J
  • ShortyShorty Manchester, UK Icrontian
    edited November 2003
    Near the bottom of the right hand corner of the Short-Media frontpage, there is a working example of the rand() function called "Short-Media Sponsors" :)

    I created a SQL table and then added the file names inside it, then query it as Park did with RAND() limit 1.

    Works well, refresh the page a few times :)
Sign In or Register to comment.