Jump to content

loops and variables


bluemicrobyte

Recommended Posts

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 array


What 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

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

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.