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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jamesm87
Forum Newbie
Posts: 21
Joined: Sat May 31, 2003 1:54 pm
Location: London, UK

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

Post 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:
User avatar
nickvh
Forum Newbie
Posts: 11
Joined: Sat May 31, 2003 3:37 pm
Location: VA, USA
Contact:

Post 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.
pootergeist
Forum Contributor
Posts: 273
Joined: Thu Feb 27, 2003 7:22 am
Location: UK

Post 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];
pootergeist
Forum Contributor
Posts: 273
Joined: Thu Feb 27, 2003 7:22 am
Location: UK

Post by pootergeist »

subnote: mysql actually has a rand function/clause of its own which might well be worth sleuthing in the online manual
Post Reply