Jump to content


Creating an array

  • Please log in to reply
2 replies to this topic

#1 ViperSBT

  • Members
  • PipPipPip
  • Advanced Member
  • 49 posts

Posted 14 June 2006 - 04:56 AM

OK, here is the scenario. I run a SQL query that generates 32 rows of results containing id, name, image.

What I need to do with this is to create and array that contains each row of data and includes an "array key" for each row.

example SQL return:
123, fred, fred.jpg
145, wilma, wilma.jpg

I want that to be an array:
1, 123, fred, fred.jpg
2, 145, wilma, wilma.jpg

I have tried several ways and have had nothing but frustration... Once the array is built how do I access the information, say I just want to pull Wilma's row and skip Fred?

#2 .josh

  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 14 June 2006 - 05:02 AM

while ($list = mysql_fetch_array($result)) {
   $blah[] = $list;

edit: sorry, forgot to show how to access:

the array keys will be the names of the columns in your database. so for instance, if wilma and fred's column name is 'name':
$x = 0;
while ($blah[$x]) {
   if ($blah[$x]['name'] == 'wilma') {
       foreach($blah[$x] as $key => $val) {
           echo $key . " : " . $val . "<br>";

Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#3 joquius

  • Members
  • PipPipPip
  • Advanced Member
  • 319 posts

Posted 14 June 2006 - 07:57 AM

while ($list = mysql_fetch_array($result)) {
$blah[] = $list;

I believe this will give results as 0=>"blah", field=>"blah" no?

mysql_fetch_assoc () ?

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users