Jump to content

[SOLVED] Inserting data from a sql database into an array


johng

Recommended Posts

I am currently working on a project where I need to put information from a sql statement into an array.  Currently I can get one row into the array, but the rest aren't showing.  I have checked, and all of the information is getting to the array, but I can only get one row to show up. 

 

Here's the code:

 

  while(($row = mysql_fetch_array($result)))
  {
   $info=array($count=>array('line1'=>$row["first_name"], 'line2'=>$row["last_name"], 'line3'=>$row["product"], 'line4'=>"\$".$row["date"]));
   $count++;
  }

 

I'm trying to get this array to hold multiple rows from the sql statement, but when I display the array, I can only get one row out of it. Any help on this would be much appreciated.

 

 

As always, thanks in advance!

If you don't mind indexes of 'first_name', 'last_name' etc  instead of 'line1', 'line2', 'line3' then this would suffice

 

while(($row = mysql_fetch_assoc($result)))
  {
   $info[] = $row;
  }

// view the info array

echo '<pre>', print_r($info, true), '</pre>';

The main reason I used 'line1', 'line2', etc. is because it was being fed into another function, and that function was set up to use those field names.  Basically it was way less complicated because I didn't want to fiddle with the other function.  But thanks for the extra info!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.