Page 1 of 1

Array's,Mysql, and the rand help please!

Posted: Sat May 31, 2003 1:54 pm
by jamesm87
Hi...

I want to select every id in a table called bb_vote where active = yes, then put the results into an array, and then choose an ID number at random.

I have made this script so far but unfortunaly it only displays ID=1 and no other ID.

Code: Select all

$sql="select id from bb_vote where active=('y')";
$result=mysql_query($sql)
	or die ("cant do check!");

$aray= mysql_fetch_array($result);
$num = mysql_num_rows($result);

$random=array_rand($aray,1);

$sql2="select id,loginName,info,pic from bb_vote where id=($arayї$random])";
$result2=mysql_query($sql2)
	or die ("cant do check!");
$all = mysql_fetch_array($result2);
$num2 = mysql_num_rows($result2);
extract ($all);
I spelled the variables Aray as I thought calling it Array may be causing a conflict.

Could someone please help me... and sorry about my punctuation and spelling! lol :lol:

Posted: Sat May 31, 2003 4:27 pm
by nickvh
Mysql_fetch_array fetches the row not the column, I think that is the prob, Idk how ta fix it though :) It looks like you are selecting a random column in a single row.

Posted: Sat May 31, 2003 6:40 pm
by pootergeist
$sql="select id from bb_vote where active=('y')";
$result=mysql_query($sql)
or die ("cant do check!");
// note changes below
while($aray= mysql_fetch_array($result))
{
$built_aray[] = $aray['id'];
}
$num = mysql_num_rows($result);
$rand = rand(0,$num);
echo $built_aray[$rand];

Posted: Sat May 31, 2003 6:42 pm
by pootergeist
subnote: mysql actually has a rand function/clause of its own which might well be worth sleuthing in the online manual