Jump to content


Photo

Create An Array Dynamically?


  • Please log in to reply
5 replies to this topic

#1 alphacooler

alphacooler
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 27 June 2006 - 08:54 PM

I am querying one column in a table and would like to create an array that holds all of the data in the rows of that column. Say the column contains usernames. I would like to have one array that has every username in it.

How would I create that array?

Thanks!

#2 thepip3r

thepip3r
  • Members
  • PipPipPip
  • Advanced Member
  • 289 posts

Posted 27 June 2006 - 08:57 PM

i'm assuming this is a column in a table in a database??? which database? if it's MySQL, use something like mysql_fetch_array() and RTFM. =D

#3 alphacooler

alphacooler
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 27 June 2006 - 09:02 PM

[!--quoteo(post=388642:date=Jun 27 2006, 03:57 PM:name=thepip3r)--][div class=\'quotetop\']QUOTE(thepip3r @ Jun 27 2006, 03:57 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
i'm assuming this is a column in a table in a database??? which database? if it's MySQL, use something like mysql_fetch_array() and RTFM. =D
[/quote]

mysql_fetch_array just creates an array with the data (in this case the username) in the first row. I need to combine all of the data from all of the rows into one array that can be searched.

#4 thepip3r

thepip3r
  • Members
  • PipPipPip
  • Advanced Member
  • 289 posts

Posted 27 June 2006 - 10:21 PM

$sql = "SELECT $column FROM $table";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
  echo $row['$column'];
}

...displays ALL columns for a specific query in a MySQL database and if you want to write those vars to an array to search:

change "echo $row['$column']" to "$array[] = $row['$column']"

#5 alphacooler

alphacooler
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 27 June 2006 - 10:30 PM

[!--quoteo(post=388661:date=Jun 27 2006, 05:21 PM:name=thepip3r)--][div class=\'quotetop\']QUOTE(thepip3r @ Jun 27 2006, 05:21 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
$sql = "SELECT $column FROM $table";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
  echo $row['$column'];
}

...displays ALL columns for a specific query in a MySQL database and if you want to write those vars to an array to search:

change "echo $row['$column']" to "$array[] = $row['$column']"
[/quote]

ThePip3r, thanks man could you tell me why this didn't work (which is what I had before)

    $myarray=array();
    for ($i=0; $i<$num_results; $i++)
    {
        $username=$row['username'];
        $myarray[]=$username;
    }


#6 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 28 June 2006 - 02:18 AM

that doesn't work because it's not inside the fetch array loop. all you are doing there is assigning the same information $num_results times to each array position.

$sql = "select * from table";
$result = mysql_query($sql);
while ($info = mysql_fetch_array($result)) {
   $blah[] = $info;
}

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 :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users