Jump to content

Archived

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

alphacooler

Create An Array Dynamically?

Recommended Posts

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!

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
[!--quoteo(post=388642:date=Jun 27 2006, 03:57 PM:name=thepip3r)--][div class=\'quotetop\']QUOTE(thepip3r @ Jun 27 2006, 03:57 PM) [snapback]388642[/snapback][/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.

Share this post


Link to post
Share on other sites
[code]
$sql = "SELECT $column FROM $table";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
  echo $row['$column'];
}[/code]

...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']"

Share this post


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

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

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

Share this post


Link to post
Share on other sites
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.

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

Share this post


Link to post
Share on other sites

×

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.