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.

Display 3 random users from DB

Featured Replies

Hello, have been running a Facebook competition and was going to draw the names from a hat.

 

Turns out a few more people than expected entered so I wish to just use a button on my web page to display 3 random users from my database.

 

I have the fields of full name & email.

 

Thanks, Craig

Fetch all the names inside one array using mySQL and PHP,

shuffle them in a random order and display the first three.

 

Léon

This.

  • Author

Thanks for the help :)

 

Managed to get it to do roughly what i wanted.

 

I could only get it to display 1 result though. How do i choose the amount it shows?

 

Heres what i have:

 

$result = mysql_query('SELECT * FROM email') or exit(mysql_error());
while ($row = mysql_fetch_assoc($result)) {$array[] = $row;}
shuffle($array);
print_r($array[3]); 

 

I thought the [3] would show 3 results? or does it show the 3rd result?

Thanks for the help :)

 

Managed to get it to do roughly what i wanted.

 

I could only get it to display 1 result though. How do i choose the amount it shows?

 

Heres what i have:

 

$result = mysql_query('SELECT * FROM email') or exit(mysql_error());
while ($row = mysql_fetch_assoc($result)) {$array[] = $row;}
shuffle($array);
print_r($array[3]);

 

I thought the [3] would show 3 results? or does it show the 3rd result?

 

It shows the 4rd result. ;)

Arrays are zero based.

 

[0] -> First result

[1] -> Second result

[2] -> Third result

 

Hope it helps,

Léon

Edited by Leonvv

Ah gotcha, so what about printing out 3 results?

 

echo $array[0];

echo $array[1];

echo $array[2];

 

Or:

print_r($array);

to see the all the elements in the array.

 

Léon

  • Author

echo $array[0];

echo $array[1];

echo $array[2];

 

Or:

print_r($array);

to see the all the elements in the array.

 

Léon

 

When someone explains the answer it always seems to simple... yet until that point im puzzled!

 

Thanks for all your help, much appreciated :)

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.