Jump to content

Put Mysql Rows in a Numeric Array


lilman

Recommended Posts

My objective:  To fetch the results of the query and then put the results into a numeric array. Then I will use the rand() function to randomly select a number and display that array value.

My mysql code is below:

[code]
$cat=$column['category'];
$smiliarpolls="SELECT * FROM polls WHERE category='$cat'";
$run=mysql_query($smiliarpolls);
[/code]


I want to display 10 random polls that meet the criteria above.  Any ideas? Thank you
Link to comment
https://forums.phpfreaks.com/topic/29483-put-mysql-rows-in-a-numeric-array/
Share on other sites

EDIT: SHOOT - I answered for one random poll... (do a while loop and array push - will give you all them in an array - then do a shuffle and take the first 10)

there is an order by rand in mysql, however it is kinda slow.

if you know you have at least 20 polls in there, you can
$num=rand(0,19) (get random number)

ORDER BY id DESC (to get the most recent)
LIMIT $num,1

That would get you a random out of your last 20

Or do two queries one with a count and then choose random from that number. but that i a bit slower again.

hope this helps...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.