Connecting Tech Pros Worldwide Help | Site Map

Fetch xth item from mysql_array

Newbie
 
Join Date: Dec 2007
Posts: 15
#1: Nov 17 '08
Hey gentlebeings,

I'm pretty sure this is possible, just can't find the correct way to do it.

I want to select the contents of my table of testimonials, and randomly display one of them on the screen.

So the thinking we had over here was to count the number of rows and then select the xth row for display. We just weren't sure on the options available to us. :)

One option was to count the rows, use that number for the basis for a random select (i.e. mt_rand(1,$num_testimonials)) and select that row.

Just not sure if that would work at all...

If I recall correctly, fetching an associative array will allow us to move forward and backwards through it, so somehow this should be possible.

I'm open to all suggestions, can't even think of a way to start this.

Thanks,

Ray
Newbie
 
Join Date: Dec 2007
Posts: 15
#2: Nov 17 '08

re: Fetch xth item from mysql_array


Duh!

Sorry people, the answer was so obvious it slapped us in the face!

Expand|Select|Wrap|Line Numbers
  1. SELECT column FROM table ORDER BY RAND() LIMIT 1 
Works like a charm!

Ray

(figures we should have just spoken to the sql guru rather than the php guru)
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,936
#3: Nov 17 '08

re: Fetch xth item from mysql_array


Glad you got that sorted.
Reply