Jump to content


Photo

randomising a quote from mysql


  • Please log in to reply
2 replies to this topic

#1 davieboy

davieboy
  • Members
  • PipPipPip
  • Advanced Member
  • 46 posts

Posted 09 June 2003 - 04:13 PM

what SELECT statement would i use to display a random quote taken from the database table??

Davi

#2 shivabharat

shivabharat
  • Members
  • PipPipPip
  • Advanced Member
  • 371 posts
  • LocationChennai, India

Posted 09 June 2003 - 06:12 PM

One way of doing that would be

like if have a unique ID to refer this quotes say u have 200 quotes and they have field say quote_id from 1 -200

u can use PHP rand() or srand() to select a number randomly and pass that to query.
Knowledge --- Reading Enriches Mind But Sharing Enhances It.[br][br]Note: Before you request help enusre that you have had a look at the tutorials @phpfreaks

#3 gizmola

gizmola
  • Administrators
  • Advanced Member
  • 4,664 posts
  • LocationLos Angeles, CA USA

Posted 09 June 2003 - 10:07 PM

I agree 100% with what Shivabharat advised. A few things I would add though:

With the more current versions of php you suppossedly don\'t need to seed rand with srand.

I would select min(quoteid), max(quoteid) from yourtable first, then use those values to pass to rand:

$randid = rand($minval, $maxval);

Then as Shiv suggested just do a select quote from yourtable where quoteid = $randid

8)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users