Jump to content

Create An Array Dynamically?


alphacooler

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!
Link to comment
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.
Link to comment
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']"
Link to comment
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]
Link to comment
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]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.