Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

how do i randomly select images from my database

Featured Replies

Hello friends,

 

pls i need to display different images at each time a visitor visit my webpage. i have my images in the database but i dont know how to each/display them at random? pls help.

 

regards

How is your database set up?

 

What are the fields?

 

If you have a database which is JUST for images and it has a unique integer ID then you could set a variable:

 

$my_image = rand(1,100);

 

and then select via SQL using something like:

 

SELECT image WHERE img_id = $my_image

 

I am no expert - but if only wanted to display a random image then do you need them in a DB? Couldn't javascript do the same job?

 

:)

Terydhino has the right answer however there is a simpler way of doing it:

 


mysql_query("SELECT image FROM table ORDER BY RAND() LIMIT 1");

 

 

That will randomly select one `image` from the table `table`.

MySQL has some weird and wonderful functions. If something looks over complicated, it probably is.

 

Thanks for the +1 :)

You will find this method below to be much much much much much much quicker than using mysql to generate the random number. You wont notice it if you only have a few hundred records but if you have thousands you will definitely want to use the method stated by terydinho below rather then the MYSQL method. Iv tried both on record sets of 10,000 and it is noticably faster to use php to generate a random number first then use that.

 

How is your database set up?

 

What are the fields?

 

If you have a database which is JUST for images and it has a unique integer ID then you could set a variable:

 

$my_image = rand(1,100);

 

and then select via SQL using something like:

 

SELECT image WHERE img_id = $my_image

 

I am no expert - but if only wanted to display a random image then do you need them in a DB? Couldn't javascript do the same job?

 

:)

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.