Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
03-30-2013, 07:26 PM #1
- Join Date
- Mar 2012
- Thanked 0 Times in 0 Posts
Pick x number of rows from mysql table and display with php
i am currently designing the welcome page for my website and i would like to display random images from my database on the home screen.
i have many images stored in my database and each time someone visits the site i would like 4 random pictures to be selected from my db and displayed. i have heard about the rand() php function but heard that for large databases this is a bad one to use.
i really am stumped on this, thanks in advance to anyone who can help
03-30-2013, 11:19 PM #2
"large" means hundreds of thousands or millions of records.
I doubt seriously that you have that many images in use.
I would feel comfortable using RAND() with up to a few thousand records, though if your tables are "wide" (that is, if there are many many fields in each row of the table) there are some tricks you might want to use to improve the efficiency of your queries.Be yourself. No one else is as qualified.
03-30-2013, 11:22 PM #3
For example, if you have an ID column as your PRIMARY KEY, you could do:
SELECT * FROM yourtable WHERE id IN ( SELECT id FROM yourtable ORDER BY RAND() LIMIT 4 )Be yourself. No one else is as qualified.