MySQL Results into Array for Future Use.

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
User avatar
Etherguy
Forum Commoner
Posts: 70
Joined: Fri Nov 01, 2002 9:09 pm
Location: Long Island, New York

MySQL Results into Array for Future Use.

Post 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??
matthiasone
Forum Contributor
Posts: 117
Joined: Mon Jul 22, 2002 12:14 pm
Location: Texas, USA
Contact:

Post 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;
User avatar
Etherguy
Forum Commoner
Posts: 70
Joined: Fri Nov 01, 2002 9:09 pm
Location: Long Island, New York

Ok...

Post 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!
matthiasone
Forum Contributor
Posts: 117
Joined: Mon Jul 22, 2002 12:14 pm
Location: Texas, USA
Contact:

Post 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
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

I like foreach loops:

Code: Select all

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