Page 1 of 1

MySQL Results into Array for Future Use.

Posted: Mon Mar 10, 2003 1:02 pm
by Etherguy
Afternoon,

Okay, got a quick one here, that I hope will have me kicking my self. What I am trying to do is pull data from a database, and store it in an Array so that I can reference it later to be displayed.

This is what I have so far, but the array only holds the first value.

Code: Select all

if(mysql_num_rows($result)){
$emailaddress = mysql_result($result,$i,"email_address");
$amount =mysql_result($result,$i,"amount");
$reason = mysql_result($result,$i,"reason");
$item = array();
while ($number > $i) {


$itemї]="$reason";
}
And here is how I am calling the Array

Code: Select all

while (list($key,$value)= each($item)){
print "$key :: $value";
}
Any Ideas??

Posted: Mon Mar 10, 2003 2:27 pm
by matthiasone
It appears as though you are doing it the hard way.

Code: Select all

$num_rows = mysql_num_rows($result);
  for ($j = 0; $j < $num_rows; $j++) 
  &#123;
    $rowdata = mysql_fetch_assoc($result);
    $emailaddress&#1111;$j]= $rowdata&#1111;'email_address'];
    $amount&#1111;$j] = $rowdata&#1111;'amount'];
    $reason&#1111;$j] = $rowdata&#1111;'reason'];
  &#125;

Ok...

Posted: Mon Mar 10, 2003 7:30 pm
by Etherguy
Okay... I see your point about doing it the long way. But do I still use :

Code: Select all

while (list($key,$value)= each($item))&#123; 
print "$key :: $value"; 
&#125;
To retrieve my data, or is there a nicer way?

Thanks!

Posted: Tue Mar 11, 2003 9:27 am
by matthiasone
personally I would use another For loop.

Code: Select all

$max = count($reasons)-1; 
  for ($j = 0; $j <= $max; $j++) 
  &#123; 
    echo $emailaddress&#1111;$j]." ::".$amount&#1111;$j] ." :: ".$reason&#1111;$j]; 
  &#125;


or what ever format you want.

but I believe the way you were doing it would work also. :D

Posted: Tue Mar 11, 2003 9:39 am
by twigletmac
I like foreach loops:

Code: Select all

foreach ($item as $key => $value) { 
    echo $key.' :: '.$value; 
}
Mac