two questions on generating a random user

They have: 461 posts

Joined: Jul 2003

$frm=mysql_query("SELECT users.gender,stats.dob,stats.sex_pref,stats.mar_stat,stats.country FROM users,stats WHERE users.uid=stats.uid AND users.uid='obviously that's imcomplete. what i'm not sure is how to randomly generate a user id number. rand() takes a seed, and i'm not going to know how many users there are prior to running this select. so i'm not sure how to randomly get a valid number (there will probably be holes as people delete from the site, etc.

also, once i have the array will it be $array['table.field'] or $array['field'] to access the array?
i haven't tried to get things form multiple tables before.
thnx in advance
-Josh

POSIX. because a stable os that doesn't have memory leaks and isn't buggy is always good.

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

SELECT * FROM users ORDER BY rand() LIMIT 1

They have: 93 posts

Joined: Jun 2003

Not sure why you want to generate a random ID, and not a somewhat sequential index with AUTO_INCREMENT , this will eliminate the HUMAN error aspect and always generate a unique ID.

In answer to your query results: $result['fieldname']

Quote: SELECT * FROM users ORDER BY rand() LIMIT 1

?? This will only select a random user, if he uses that ID whats to say its not 3, and someone is #4, then he would be given an error is he made it a unique field, or he would have two entries under the same ID.

To get your last highest ID

SELECT id FROM users ORDER BY id DESC LIMIT 1

[Design Alpha] -Web Services : Design,Hosting,Advertising,Software
Ask about custom pricing on hosting!!
Site Assets: [UltraGaming.com] [Blades of Warcraft]

They have: 461 posts

Joined: Jul 2003

shane: i have a database with user id numbers assigned when you sign up.

some people may delete, some may be deleted. this leaves holes.

for the front page i want to seelct a user at random and pull some information for a section.

so i want to pull a randome user between 1 and an unknown maximum.

i'm hoping to find a way to do this so that it'll change the user displayed with each load of the index.

POSIX. because a stable os that doesn't have memory leaks and isn't buggy is always good.

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

SELECT users.gender,stats.dob,stats.sex_pref,stats.mar_stat,stats.country
FROM users
INNER JOIN stats ON users.uid=stats.uid
ORDER BY rand()
LIMIT 1

Mark Hensler
If there is no answer on Google, then there is no question.

They have: 461 posts

Joined: Jul 2003

i think i understand what's happening. i'd like to make sure i do understand what's happening there.

Quote: SELECT fields

selects the feilds i want to display

Quote: FROM users

means to search by the users table

Quote: INNER JOIN stats ON users.uid=stats.uid

means to synchronise based on the uid fields

Quote: ORDER BY rand()

makes it randomly take the users on the ... for lack of a better way to think of it, the temporary table it made

Quote: LIMIT 1

makes it take just one of them.

i've never used a join before, or the rand function, which is why i want to make sure i understand what's going on

sorry about asking more questions, it's just how i do things with programming/scripting. when i ask for help, if i'm using things that are new to me i break it down and verify that i'm understanding each part, so that i know i'll be learning what's goingon correctly. then i can help others in the future

POSIX. because a stable os that doesn't have memory leaks and isn't buggy is always good.

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.