Jump to content


Photo

Can I get a random row. with mySQL


  • Please log in to reply
4 replies to this topic

#1 mazman13

mazman13
  • Members
  • PipPipPip
  • Advanced Member
  • 156 posts

Posted 14 September 2006 - 11:30 PM

Ok, I have a script that picks a random number based on the number of banners in my database.

The number is used to pick a rank number in the banners.

example: random number 1, so it picked picture.jpg (rank no. 1) from the database and plays it.

Is there a way, to just have it pick a row number.

example: SELECT * FROM database WHERE COL NUMBER IS (random number from script)

cuz the way i have it now, if i erase a banner now, the image rank is erased, so some banners might now be picked. can i get info from a row number? i hope people can understand what i'm saying...

ask questions if you don't understand...i havn't slept much this week.

#2 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 14 September 2006 - 11:49 PM

SELECT * from $tablename ORDER by RAND LIMIT 1
Legend has it that reading the manual never killed anyone.
My site

#3 mazman13

mazman13
  • Members
  • PipPipPip
  • Advanced Member
  • 156 posts

Posted 14 September 2006 - 11:50 PM

Wow! thats it? I don't even need that random PHP script huh? it will pick somethin on random huh?

#4 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 14 September 2006 - 11:51 PM

Sorry it's so simple, but yes that will pick a single random row without any help from anything else ;D
Legend has it that reading the manual never killed anyone.
My site

#5 mazman13

mazman13
  • Members
  • PipPipPip
  • Advanced Member
  • 156 posts

Posted 15 September 2006 - 03:55 PM

Code works great, just one lil' thing to fix to make it work:
SELECT * FROM $tablename ORDER BY RAND() LIMIT 1




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users