Displaying Random Images from a mySQL Database
Josh-
Royal Oak, MI
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]
[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]
0
Comments
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.
SELECT * FROM gallery ORDER BY RAND()
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
Well, thanks.
-J
Thank you also Park..
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.
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
-J
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