Rauvnos Posted August 30, 2008 Share Posted August 30, 2008 Heya folks, new to phpfreaks. I just started learning php a couple of weeks ago. I tried searching for a resolution on these boards first, but couldn't quite find what I was looking for. I'm pulling a single column of data from a query. I need to pull a random value out of that. So, let's say I had a table 'people' and I was pulling their 'id' column. I want to grab a random ID out of that column. I've tried using array_rand against the result of the query, but couldn't figure out how to do it right. Mind, I'm *only* pulling that one column. So I don't think I need to worry about stripping out other columns from the results. Thoughts? (And thanks in advance.) Link to comment https://forums.phpfreaks.com/topic/122017-fetching-a-random-value-from-query-results/ Share on other sites More sharing options...
Rauvnos Posted August 30, 2008 Author Share Posted August 30, 2008 Nevermind on this one. Since I'd be pulling from less than 1k records, I just tossed an ORDER BY rand() into the SQL. Does the trick. Link to comment https://forums.phpfreaks.com/topic/122017-fetching-a-random-value-from-query-results/#findComment-629845 Share on other sites More sharing options...
DarkWater Posted August 30, 2008 Share Posted August 30, 2008 You might want to rework it in the future because it's pretty slow. Link to comment https://forums.phpfreaks.com/topic/122017-fetching-a-random-value-from-query-results/#findComment-629851 Share on other sites More sharing options...
redarrow Posted August 30, 2008 Share Posted August 30, 2008 quick mysql example SELECT i, RAND(3) FROM t; Link to comment https://forums.phpfreaks.com/topic/122017-fetching-a-random-value-from-query-results/#findComment-629861 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.