Re: problems with random numbers

From: Date: Fri, 01 Dec 2000 01:16:41 +0000
Subject: Re: problems with random numbers
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-28141@lists.php.net to get a copy of this message
hI, Why don't you use this instead? $a = 0; $rand_text_result = query("SELECT * FROM poems"); while ($row = mysql_fetch_array($rand_text_result)) { $arrResults[$a] = $row["id"]; ++$a; } //using this function: mixed array_rand (array input [, int num_req]) // NB:_ only available with PHP4 $arrResultsRand = mixed array_rand ($arrResults, 3); echo $arrResultsRand[0] . "<br>"; echo $arrResultsRand[1] . "<br>"; echo $arrResultsRand[2] . "<br>"; ?> Broder B ha scritto: > I am trying to three random generated numbers so I can get three diferent queries to my MySQL > database. The problem I have is that sometimes I only get two numbers instead of three, I sense the > problem is that one of the randomized number does not exists in the database. Here is the code, > maybe someone could help me? > > srand ((double) microtime() * 1000000); > $randval1 = rand(1, $num_rows); > $randval2 = rand(1, $num_rows); > $randval3 = rand(1, $num_rows); > > $rand_text_result = query("SELECT * FROM poems WHERE id=$randval1 or id=$randval2 or > id=$randval3"); > while ($row = mysql_fetch_array($rand_text_result)) { > echo $row["id"] . "<br>"; > }

« previous php.general (#28141) next »