Jump to content

[help] How to select a random field from a column

- - - - -

  • Please log in to reply
4 replies to this topic

#1
Omar Reyes

Omar Reyes

    Newbie

  • Members
  • Pip
  • 1 posts
I need help with a project I am working in. How can I choose a random field from a column? For example, in the attached picture it has id, username, password, and credits as the columns. I want to choose one of the random credits and print it in a php page.

There will be thousands of columns so it has to be really random.

Thank you very much!

Attached File  mysql.png   17.45K   31 downloads

#2
amrosama

amrosama

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 8,674 posts

select `credits` from `table_name` order by rand();


yo homie i heard you like one-line codes so i put a one line code that evals a decrypted one line code that prints "i love one line codes"
eval(base64_decode("cHJpbnQgJ2kgbG92ZSBvbmUtbGluZSBjb2Rlcyc7"));
www.amrosama.com | the unholy methods of javascript

#3
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
  • Location:Vancouver, Eh! Cleverness: 200
I believe you are referring to rows rather than columns. A column is "name", a field is "john", a row is a row of columns.

You could use LIMIT with the last query, to only retrieve one row (rather than all of them
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#4
fayyazlodhi

fayyazlodhi

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 403 posts
or you could use TOP key word like Select TOP 20 etc. i.e. first 20 rows meeting a particular criteria

Edited by fayyazlodhi, 11 June 2011 - 12:07 PM.
clearifying


#5
MIMi King

MIMi King

    Newbie

  • Members
  • Pip
  • 5 posts
i think you could user shuffle! but im not sure... if i was you i would generate a random number between 0 and mysql_num_rows($query_string) then run the for loop with while limit of the random number then in the loop declare $credit=$credit_query['credits']; then print the last output.....

the code will look like this:

$query_string=mysql_query('asas');

$rows=mysql_num_rows($query_string);

$random_num=mt_rand(0,$rows);

for($i=0; $i<$random_num; $i++) {

$credit_query=mysql_fetch_assoc($query_string);

$credit=$credit_query['credits'];

}

echo $credit;

although random is something that is not always random... thus it still do depend on your random generating algorithm.... this function generates a satisfying random .... but it's not sure.. even in facebook sometimes you see someone 1000 times while never seeing other-one!
and something don't store your password that kind... encrypt it with md5 or something else that would protect it... then for login compare the md5-ed password input with the one in db....

BB




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users