bluemicrobyte Posted June 6, 2006 Share Posted June 6, 2006 The following code:[code] $imagefilearray = array(); while ($row = mysql_fetch_row($result)) { $imagefilearray = array_push($imagefilearray, $row[2]); } $newimage = array_rand($imagefilearray); [/code]Gives me:Warning: array_rand(): First argument has to be an arrayWhat I'm trying to do is put all the $row[2]s in an array then select a random item from that array. Link to comment https://forums.phpfreaks.com/topic/11292-loops-and-variables/ Share on other sites More sharing options...
kenrbnsn Posted June 6, 2006 Share Posted June 6, 2006 Try this instead:[code]<?php $imagefilearray = array(); while ($row = mysql_fetch_row($result)) $imagefilearray[] = $row[2]; $newimage = array_rand($imagefilearray);?>[/code]Ken Link to comment https://forums.phpfreaks.com/topic/11292-loops-and-variables/#findComment-42265 Share on other sites More sharing options...
poirot Posted June 6, 2006 Share Posted June 6, 2006 If you just want to pull a random image from the db, use only MySQL, it will be way faster - especially if you have a lot of records:[code]$res = mysql_query("SELECT * FROM images ORDER BY RAND() LIMIT 1") or die(mysql_error());$newimage = mysql_result($res, 2);[/code] Link to comment https://forums.phpfreaks.com/topic/11292-loops-and-variables/#findComment-42273 Share on other sites More sharing options...
bluemicrobyte Posted June 6, 2006 Author Share Posted June 6, 2006 Thanks, poirot, that works nicely. Link to comment https://forums.phpfreaks.com/topic/11292-loops-and-variables/#findComment-42296 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.